I know I once know how to do this but... how do you run a script (bash is OK) on login in unix?
问题:
回答1:
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.
回答2:
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:
- bash: .bash_profile, .bash_login, .profile (for backwards compabitibility)
- sh: .profile
- tcsh and csh: .login
- zsh: .zshrc
You can probably find out what shell you're using by doing
echo $SHELL
from the prompt.
回答3:
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.
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
.
回答4:
If you wish to run one script and only one script, you can make it that users default 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 )
回答5:
If you are on OSX, then it's ~/.profile
回答6:
Place it in your bash profile:
~/.bash_profile
回答7:
Launchd
is a the preferred way in OS X.
If you want it to run on your login put it in ~/Library/LaunchAgents
Start launchd
item
launchctl load /Library/LaunchDaemons/com.bob.plist
Stop item
launchctl unload /Library/LaunchDaemons/com.bob.plist
Example 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>
回答8:
I was frustrated with this problem for days. Nothing worked on ubuntu. If I put the call in /etc/profile it all crashed at login attempt. I couldn't use "Startup Applications" as that was not what I wanted. That only sets the script for that current user.
Finally I found this little article: http://standards.freedesktop.org/autostart-spec/autostart-spec-0.5.html
The solution would be:
find out the $XDG_CONFIG_DIRS path:
echo $XDG_CONFIG_DIRS
put your script in that directory
回答9:
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.
回答10:
Search your local system's bash man page for ^INVOCATION for information on which file is going to be read at startup.
man bash
/^INVOCATION
Also in the FILES section,
~/.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.
回答11:
The script ~/.bash_profile
is run on login.