I know I once know how to do this but... how do you run a script (bash is OK) on login in unix?
11 Answers
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.
- 34,335
- 35
- 194
- 277
- 3,555
- 8
- 28
- 23
-
4only a decade late, but: what if that command requires sudo (e.g. mounting a network share from a NAS into the user's home dir) – Mike 'Pomax' Kamermans Dec 19 '18 at 22:01
-
@Mike'Pomax'Kamermans excellent question, maybe the Sudo is assumed since it's in the system' files? – Webwoman Feb 28 '19 at 12:52
-
maybe, but maybe not - hopefully someone can still answer that for us =( – Mike 'Pomax' Kamermans Feb 28 '19 at 15:59
-
`sudo` is an optional add-on, nothing much will run it unless you specifically (install it and) ask for it. If your command requires noninterctive `sudo` privileges, check the `sudo` documentation for how to set that up. – tripleee Oct 26 '22 at 12:01
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.
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.
- 41,842
- 6
- 48
- 60
-
1echo $0 should reveal which shell is being used, although occasionally I've seen 'sh' reported, when it's really 'ksh' - on HP-UX or Solaris I think. – dr-jan Sep 19 '08 at 23:22
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.
- 2,287
- 16
- 21
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 )
- 56,416
- 14
- 107
- 150
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>
- 6,472
- 5
- 44
- 50
- 14,473
- 16
- 63
- 82
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
- 1,426
- 5
- 28
- 55
Place it in your bash profile:
~/.bash_profile
- 74,723
- 23
- 102
- 147
- 51,617
- 12
- 104
- 148
If you are on OSX, then it's ~/.profile
- 74,723
- 23
- 102
- 147
- 553
- 1
- 4
- 10
-
This is not entirely correct. `.profile` is run by interactive Bourne shell instances, regardless of OS; but if yours is not a run of the mill Bourne shell, maybe it won't. Bash out of the box will look for `.bash_profile` and if it is not found fall back to `.profile`. A common arrangement if you do have `.bash_profile` is to include an instruction to _also_ load `.profile` if present. These days, MacOS out of the box defaults to Zsh, not Bash, which has still different logic. – tripleee Oct 26 '22 at 11:59
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.
- 64,444
- 15
- 143
- 197
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.
- 8,228
- 2
- 43
- 37
The script ~/.bash_profile is run on login.
- 74,723
- 23
- 102
- 147
- 5,256
- 1
- 25
- 22