11

I am trying to write bash shell script in Ubuntu 11.10 Linux distro, that will get executed automatically on logging into the system. But I am not able to figure out that what to write in script that by it will get automatically executed on logging in.

15

If you want it to be global, modify

 /etc/profile 

or add a script to

 /etc/profile.d

If you want it to be user-specific, modify

 /home/$USER/.profile
  • 1
    Don't forget that the extension should be .sh if you put your script inside of /etc/profile.d/. chmode +x /etc/profile.d/myscript.sh of course too. – Brian Haak Aug 4 '17 at 22:59
  • Raspbian Stretch. Trying to run /usr/local/bin/pihole -c command on autologin of specific user. Added this line to /home/$USER/.profile - nothing happens. When I add script to /etc/profile.d then it works, but it's global and runs it even when I open ssh session(which is not what I want). Adding to .bashrc works too, but runs any time I open new shell (which is no what I want). Question: Why adding to /home/$USER/.profile wouldn't work??? – Drew Apr 18 '18 at 8:08
3

Just add the bash script to the Startup applications.

In the command field, enter

bash /full/path/to/bash/script.sh/
  • 2
    How do you add "bash /.../script.sh" to the Startup applications from the command line? – aleb Feb 1 '13 at 20:02
  • @aleb Who said anything about adding to Startup app from command line? I am just describing the way to do it using the GUI. If you want to try using command line, add a .desktop file in /home/$USER/.config/autostart. – jokerdino Feb 2 '13 at 10:00
  • 1
    OP never mentioned a GUI; what if they're on a remote server via SSH and don't have a GUI? None of the desktop tags were applied to the question... – Doktor J Jul 10 at 20:48
0

If you want to be more bash specific you can also write you code in ~/.bash_profile or ~/.bash_login.

And you can source any script therein for example:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi
0

/etc/profile or $HOME/.profile or $HOME/.bash_profile

I would highly recommend against using /etc/profile.d/yourscript.sh if it produces output. When you use a non-interactive session, you will receive a $TERM is not set message. This is noticeable when using the ssh protocol, like scp. Usually not a big deal, however, Veeam doesn't like it and will throw a warning. I know Veeam is not the topic here, but it's worth mentioning that not all applications will gracefully ignore the $TERM is not set warning.

In short, if the script generates output, place it in the locations specified on the first line. However, if you're modifying the environment and your script doesn't generate output, then use the latter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.