How do you run a script at * nix login?

I know that I once know how to do this, but ... how do you run a script (bash OK) when entering unix?

+67
linux unix bash shell macos
Sep 18 '08 at 21:24
source share
11 answers

From wikipedia Bash

When Bash starts, it executes commands in different scripts.

When Bash is called as an interactive login, it first reads and executes commands from the / etc / profile file, if that file exists. After reading this file, it searches 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 the login shell completes, Bash reads and executes commands from the ~ / .bash_logout file, if one exists.

When the interactive shell, which is not the login shell, is launched, Bash reads and executes commands from ~ / .bashrc if this file exists. This can be with the --norc option. The -rcfile file option forces Bash to read and execute commands from instead of ~ / .bashrc.

+107
Sep 18 '08 at 21:29
source share

Upon entering the system, most shells execute a login script that you can use to execute your own script. The login script executed by the shell depends, of course, on the shell:

  • bash: .bash_profile, .bash_login, .profile (for backward compatibility)
  • sh: .profile
  • tcsh and csh: .login
  • zsh: .zshrc

You can probably find out which shell you are using by doing

echo $SHELL 

from the prompt.

For a broader definition of 'login', it is useful to know that on most distributions, when X starts, your .xsessionrc will execute when the X session starts.

+27
Sep 18 '08 at 22:10
source share

When using Bash, the first of ~/.bash_profile , ~/.bash_login and ~/.profile will be launched for the interactive login shell. I believe that ~/.profile usually run by Unix shells other than Bash. Bash will run ~/.bashrc for an interactive shell without logging in.

I usually put everything that I always want to install in .bashrc , and then run it from .bash_profile , where I also create a few things that should only start when I log in, for example, setting up ssh-agent or running screen .

+9
Sep 18 '08 at 21:33
source share

If you want to run one script and only one script, you can do this by default.

 echo "/usr/bin/uptime" >> /etc/shells vim /etc/passwd * username:x:uid:grp:message:homedir:/usr/bin/uptime 

can have interesting effects :) (its not safe, so don't trust it too much. It doesn't seem like your default shell should be a script that erases your disk ... although, I can imagine a scenario where it could be surprisingly useful)

+5
Sep 18 '08 at 21:34
source share

Put it in your bash profile :

 ~/.bash_profile 
+3
Sep 18 '08 at 21:25
source share

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

+3
Sep 18 '08 at 21:26
source share

Launchd is the preferred way in OS X.

If you want it to run in 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> 
+3
Sep 19 '08 at 14:06
source share

I was disappointed with this problem for several days. Nothing worked on ubuntu. If I put the call in / etc / profile, it all crashed when I tried to login. I could not use “Launcher Applications”, as that was not what I wanted. This installs only the script for this current user.

Finally, I found this little article: http://standards.freedesktop.org/autostart-spec/autostart-spec-0.5.html

Decision:

  • find out the path of $ XDG_CONFIG_DIRS:

    echo $ XDG_CONFIG_DIRS

  • put your script in this directory

+3
Apr 29 '15 at 11:27
source share

Add an entry to /etc/profile that executes the script. This will be done during every login. If you only do this for your account, use one of your login scripts (e.g. .bash_profile ) to run it.

+2
Sep 18 '08 at 21:44
source share

Find your local bash system man page for ^ INVOCATION for information on which file will 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 desired file. Make sure the script is in $ PATH or using the absolute path to the script file.

+2
Sep 19 '08 at 0:24
source share

script ~/.bash_profile launched at login.

+1
Sep 18 '08 at 21:24
source share



All Articles