72

I know I once know how to do this but... how do you run a script (bash is OK) on login in unix?

Chris Upchurch
  • 14,953
  • 6
  • 49
  • 66
Nate
  • 17,918
  • 26
  • 69
  • 93

11 Answers11

114

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.

AlikElzin-kilaka
  • 31,903
  • 30
  • 182
  • 264
Svet
  • 3,525
  • 8
  • 26
  • 23
30

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.

pjz
  • 40,038
  • 5
  • 47
  • 60
  • 1
    echo $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
10

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.

Michael Johnson
  • 2,227
  • 16
  • 20
5

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 )

Kent Fredric
  • 55,210
  • 14
  • 104
  • 149
3

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>
Skynet
  • 6,048
  • 5
  • 44
  • 49
Milhous
  • 14,261
  • 16
  • 61
  • 80
3

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:

  1. find out the $XDG_CONFIG_DIRS path:

    echo $XDG_CONFIG_DIRS

  2. put your script in that directory

user568021
  • 1,357
  • 4
  • 23
  • 51
3

Place it in your bash profile:

~/.bash_profile
jaypal singh
  • 71,025
  • 22
  • 98
  • 142
gbjbaanb
  • 50,417
  • 11
  • 102
  • 147
3

If you are on OSX, then it's ~/.profile

jaypal singh
  • 71,025
  • 22
  • 98
  • 142
Craig B.
  • 553
  • 1
  • 4
  • 10
2

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.

ConcernedOfTunbridgeWells
  • 62,113
  • 15
  • 140
  • 195
2

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.

jtimberman
  • 8,248
  • 2
  • 42
  • 37
1

The script ~/.bash_profile is run on login.

jaypal singh
  • 71,025
  • 22
  • 98
  • 142
William Keller
  • 5,148
  • 1
  • 24
  • 22