I know I once know how to do this but... how do you run a script (bash is OK) on login in unix?
From wikipedia http://en.wikipedia.org/wiki/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.
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
.
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 )
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.
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.
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.
Launchd is a the prefered 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>