Linux 你如何在 *nix 登录时运行脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/97137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you run a script on login in *nix?
提问by Nate
I know I once know how to do this but... how do you run a script (bash is OK) on login in unix?
我知道我曾经知道如何执行此操作,但是...如何在 unix 中登录时运行脚本(bash 可以)?
采纳答案by Svet
From wikipedia Bash
When Bash starts, it executes the commands in a variety of different scripts.
When Bash is invoked as an interactive login shell, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.
当 Bash 启动时,它会执行各种不同脚本中的命令。
当 Bash 作为交互式登录 shell 被调用时,它首先从文件 /etc/profile 中读取并执行命令(如果该文件存在)。读取该文件后,它会按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,并从第一个存在且可读的命令中读取和执行命令。
当登录 shell 退出时,Bash 从文件 ~/.bash_logout(如果存在)读取并执行命令。
当一个不是登录 shell 的交互式 shell 启动时,Bash 从 ~/.bashrc 读取并执行命令(如果该文件存在)。这可以通过使用 --norc 选项来禁止。--rcfile 文件选项将强制 Bash 从文件而不是 ~/.bashrc 读取和执行命令。
回答by William Keller
The script ~/.bash_profile
is run on login.
该脚本~/.bash_profile
在登录时运行。
回答by gbjbaanb
回答by Craig B.
If you are on OSX, then it's ~/.profile
如果你在 OSX 上,那么它是 ~/.profile
回答by Michael Johnson
When using Bash, the first of ~/.bash_profile
, ~/.bash_login
and ~/.profile
will be run for an interactive login shell. I believe ~/.profile
is generally run by Unix shells besides Bash. Bash will run ~/.bashrc
for a non-login interactive shell.
使用 Bash 时,第一个~/.bash_profile
,~/.bash_login
和~/.profile
将为交互式登录 shell 运行。我相信~/.profile
除了 Bash 之外,它通常由 Unix shell 运行。Bash 将~/.bashrc
针对非登录交互式 shell 运行。
I typically put everything I want to always set in .bashrc
and then run it from .bash_profile
, where I also set up a few things that should run only when I'm logging in, such as setting up ssh-agent
or running screen
.
我通常把我想设置的所有东西都放在里面.bashrc
然后运行它.bash_profile
,我还设置了一些只有在我登录时才运行的东西,比如设置ssh-agent
或运行screen
.
回答by Kent Fredric
If you wish to run onescript and onlyone script, you can make it that users default shell.
如果您希望运行一个脚本并且只运行一个脚本,您可以将其设为用户默认的 shell。
echo "/usr/bin/uptime" >> /etc/shells
vim /etc/passwd
* username:x:uid:grp:message:homedir:/usr/bin/uptime
can have interesting effects :) ( its not secure tho, so don't trust it too much. nothing like setting your default shell to be a script that wipes your drive. ... although, .. I can imagine a scenario where that could be amazingly useful )
可能会产生有趣的效果:)(它不安全,所以不要太相信它。没有什么比将默认 shell 设置为擦除驱动器的脚本更合适。...虽然,.. 我可以想象一个场景可能非常有用)
回答by ConcernedOfTunbridgeWells
Add an entry in /etc/profile
that executes the script. This will be run during every log-on. If you are only doing this for your own account, use one of your login scripts (e.g. .bash_profile
) to run it.
在/etc/profile
其中添加一个执行脚本的条目。这将在每次登录期间运行。如果您只是为自己的帐户执行此操作,请使用您的登录脚本之一(例如.bash_profile
)来运行它。
回答by pjz
At login, most shells execute a login script, which you can use to execute your custom script. The login script the shell executes depends, of course, upon the shell:
在登录时,大多数 shell 会执行一个登录脚本,您可以使用它来执行您的自定义脚本。shell 执行的登录脚本当然取决于 shell:
- bash: .bash_profile, .bash_login, .profile (for backwards compabitibility)
- sh: .profile
- tcsh and csh: .login
- zsh: .zshrc
- bash: .bash_profile, .bash_login, .profile (为了向后兼容)
- sh:.profile
- tcsh 和 csh:.login
- zsh:.zshrc
You can probably find out what shell you're using by doing
您可能可以通过执行以下操作找出您正在使用的外壳
echo $SHELL
from the prompt.
从提示。
For a slightly wider definition of 'login', it's useful to know that on most distros when X is launched, your .xsessionrc will be executed when your X session is started.
对于“登录”的更宽泛的定义,了解在大多数发行版中当 X 启动时,您的 .xsessionrc 将在您的 X 会话启动时执行是很有用的。
回答by jtimberman
Search your local system's bash man page for ^INVOCATION for information on which file is going to be read at startup.
在本地系统的 bash 手册页中搜索 ^INVOCATION 以获取有关在启动时将读取哪个文件的信息。
man bash
/^INVOCATION
Also in the FILES section,
同样在 FILES 部分,
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
Add your script to the proper file. Make sure the script is in the $PATH, or use the absolute path to the script file.
将您的脚本添加到正确的文件中。确保脚本在 $PATH 中,或使用脚本文件的绝对路径。
回答by Milhous
Launchd
is a the preferred way in OS X.
Launchd
是 OS X 中的首选方式。
If you want it to run on your login put it in ~/Library/LaunchAgents
如果您希望它在登录时运行,请将其输入 ~/Library/LaunchAgents
Start launchd
item
开始launchd
项目
launchctl load /Library/LaunchDaemons/com.bob.plist
Stop item
停止项目
launchctl unload /Library/LaunchDaemons/com.bob.plist
Example 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>