How do you run a script on login in *nix?

161,457

Solution 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.

Solution 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.

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.

Solution 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.

Solution 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 )

Solution 5

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

Share:
161,457
Nate
Author by

Nate

A stereotypical, coffee-loving engineer from Melbourne Beach, FL

Updated on July 05, 2022

Comments

  • Nate
    Nate almost 2 years

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

  • dr-jan
    dr-jan almost 16 years
    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.
  • Mike 'Pomax' Kamermans
    Mike 'Pomax' Kamermans over 5 years
    only 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)
  • Webwoman
    Webwoman over 5 years
    @Mike'Pomax'Kamermans excellent question, maybe the Sudo is assumed since it's in the system' files?
  • Mike 'Pomax' Kamermans
    Mike 'Pomax' Kamermans over 5 years
    maybe, but maybe not - hopefully someone can still answer that for us =(