我知道我曾经知道如何做到这一点,但是... 如何运行脚本(bash 是确定的)登录在 Unix?
登录时运行脚本 ~/.bash_profile。
~/.bash_profile
把它放在你的 Bash 配置文件:
如果你在 OSX,那么它是 ~/.profile
~/.profile
来自 维基百科 Bash
当 Bash 启动时,它以各种不同的方式执行命令 剧本。 当 Bash 作为交互式调用时 登录 shell,它首先读取和 执行文件中的命令 /etc/profile,如果该文件存在的话。 在读取该文件之后,它将查找 ~/. bash _ profile、 ~/. bash _ login 和 按照这个顺序,然后读取 并执行第一个命令 一个存在并且可读的。 当登录 shell 退出时,Bash 读取 并从文件执行命令 ~/. bash _ logout,如果存在的话。 当交互式 shell 不是 启动登录 shell 后,Bash 将读取 并执行来自 ~/. bashrc 的命令, 如果该文件存在。这可能是 禁止使用—— norc 选项。 Rcfile 选项将强制执行 从中读取和执行命令 而不是 ~/. bashrc。
当 Bash 启动时,它以各种不同的方式执行命令 剧本。
当 Bash 作为交互式调用时 登录 shell,它首先读取和 执行文件中的命令 /etc/profile,如果该文件存在的话。 在读取该文件之后,它将查找 ~/. bash _ profile、 ~/. bash _ login 和 按照这个顺序,然后读取 并执行第一个命令 一个存在并且可读的。
当登录 shell 退出时,Bash 读取 并从文件执行命令 ~/. bash _ logout,如果存在的话。
当交互式 shell 不是 启动登录 shell 后,Bash 将读取 并执行来自 ~/. bashrc 的命令, 如果该文件存在。这可能是 禁止使用—— norc 选项。 Rcfile 选项将强制执行 从中读取和执行命令 而不是 ~/. bashrc。
在使用 Bash 时,将为交互式登录 shell 运行 ~/.bash_profile、 ~/.bash_login和 ~/.profile中的第一个。我相信除了 Bash 之外,~/.profile通常是由 Unix shell 运行的。Bash 将为非登录交互式 shell 运行 ~/.bashrc。
~/.bash_login
~/.bashrc
我通常把所有想要设置的内容都放在 .bashrc中,然后从 .bash_profile运行它,在 .bash_profile中我还设置了一些只有在登录时才能运行的内容,比如设置 ssh-agent或运行 screen。
.bashrc
.bash_profile
ssh-agent
screen
如果您希望运行 一脚本和 只有一个脚本,您可以使其成为用户的默认 shell。
echo "/usr/bin/uptime" >> /etc/shells vim /etc/passwd * username:x:uid:grp:message:homedir:/usr/bin/uptime
可以产生有趣的效果:)(它不安全,所以不要太相信它。)。没有什么比将默认 shell 设置为清除驱动器的脚本更好的了。虽然。.我可以想象一个场景,在那里这可能是非常有用的)
在 /etc/profile中添加一个执行脚本的条目。这将在每次登录时运行。如果您只是为自己的帐户执行此操作,请使用一个登录脚本(例如 .bash_profile)来运行它。
/etc/profile
在登录时,大多数 shell 会执行一个登录脚本,您可以使用该脚本来执行自定义脚本。Shell 执行的登录脚本当然取决于 shell:
通过执行以下操作,您可能会发现您正在使用什么 shell
echo $SHELL
从提示。
对于稍微宽泛一点的“登录”定义,了解一下在大多数发行版启动 X 时,您的。Xsessionrc 将在 X 会话启动时执行。
在本地系统的 bash 手册页上搜索 ^ INVOCATION,以获取在启动时将读取哪个文件的信息。
man bash /^INVOCATION
同样在文件部分,
~/.bash_profile The personal initialization file, executed for login shells ~/.bashrc The individual per-interactive-shell startup file
将脚本添加到适当的文件中。确保脚本在 $PATH 中,或者使用脚本文件的绝对路径。
Launchd是 OS X 中的首选方式。
Launchd
如果你想让它运行在你的登录把它放在 ~/Library/LaunchAgents
~/Library/LaunchAgents
启动 launchd项目
launchd
launchctl load /Library/LaunchDaemons/com.bob.plist
停止项目
launchctl unload /Library/LaunchDaemons/com.bob.plist
例子 com.bob.plist
com.bob.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.bob</string> <key>RunAtLoad</key> <true/> <key>ProgramArguments</key> <array> <string>/usr/bin/java</string> <string>-jar</string> <string>/Users/user/program.jar</string> </array> </dict> </plist>
这个问题困扰了我好几天。Ubuntu 上什么都没用。如果我将调用放入/etc/profile 中,它会在尝试登录时崩溃。我不能使用“启动应用程序”,因为那不是我想要的。只为当前用户设置脚本。
最后我找到了这篇小文章: http://standards.freedesktop.org/autostart-spec/autostart-spec-0.5.html
解决办法是:
查找 $XDG _ CONFIG _ DIRS 路径:
回显 $XDG _ CONFIG _ DIRS
把你的脚本放在那个目录里