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

LinuxBashMacosUnixShell

Linux Problem Overview


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

Linux Solutions


Solution 1 - Linux

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 - Linux

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 - Linux

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 - Linux

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 - Linux

Place it in your bash profile:

~/.bash_profile

Solution 6 - Linux

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

Solution 7 - Linux

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>

Solution 8 - Linux

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

Solution 9 - Linux

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.

Solution 10 - Linux

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.

Solution 11 - Linux

The script ~/.bash_profile is run on login.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNateView Question on Stackoverflow
Solution 1 - LinuxSvetView Answer on Stackoverflow
Solution 2 - LinuxpjzView Answer on Stackoverflow
Solution 3 - LinuxMichael JohnsonView Answer on Stackoverflow
Solution 4 - LinuxKent FredricView Answer on Stackoverflow
Solution 5 - LinuxgbjbaanbView Answer on Stackoverflow
Solution 6 - LinuxCraig B.View Answer on Stackoverflow
Solution 7 - LinuxMilhousView Answer on Stackoverflow
Solution 8 - Linuxuser568021View Answer on Stackoverflow
Solution 9 - LinuxConcernedOfTunbridgeWellsView Answer on Stackoverflow
Solution 10 - LinuxjtimbermanView Answer on Stackoverflow
Solution 11 - LinuxWilliam KellerView Answer on Stackoverflow