47

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.

1
  • 1
    The solution would vary depending on what "log in" actually means. If it means "starting a login shell (in a terminal, for example)", then most of the answers below would help, but if you mean that the script should run as soon as you log into a graphical desktop environment, without ever starting a terminal or shell session, then you may want to clarify this in the question. Commented Apr 19, 2023 at 4:19

6 Answers 6

67

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

 ~/.profile
4
  • 4
    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. Commented Aug 4, 2017 at 22:59
  • 1
    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??? Commented Apr 18, 2018 at 8:08
  • 1
    @BrianCannard it should be corrected as chmod +x /etc/profile.d/myscript.sh Commented May 13, 2022 at 1:21
  • This would run the script every time the user starts a login shell, regardless of whether they have just logged in or not. It is unclear whether it would run when the user logs in (e.g. via a graphical display manager; it would depend on the setup of their system). Commented Apr 19, 2023 at 4:23
4

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

2
  • Whether or not this would be executed when the user logs in depends on whether the user starts a login shell or not (which they might not do if they log in via a graphical display manager). It is unclear from the question what the user means by "logging in". In any case, your suggestion would mean the script is run each time the user starts a login script, not just when logging in. Commented Apr 19, 2023 at 4:26
  • What is meant by 'produce output'? A missing TERM would not allow graphical output to the X display, but output on stdout should be fine. Commented May 11 at 6:20
2

In Debian/Ubuntu, just add your script to System settings > Workspace > Startup and Shutdown > Autostart.

This setting is written to smth like ~/.config/autostart/myscript.sh.desktop.

Google says this works with default Window Managers in KDE, Gnome, Mate etc., but doesn't work with WMs without Session Manager like Leftwm, Spectrwm, xmonad, bspwm, dwm, i3wm, etc. For those one can try to autostart stuff by adding systemd targets and make them depend on graphical enviroment (Gnome, etc.), which is also run by systemd.

3
  • Does this really run the script upon login?   (And, can you be specific about what kind of “login”?)   Or is it only system startup, as the name suggests? Commented Aug 28, 2024 at 20:46
  • The setting is written to smth like ~/.config/autostart/myscript.sh.desktop, so yes, it's a login stuff. Google says this works with default Window Managers in KDE, Gnome, Mate etc., but doesn't work with WMs without Session Manager like Leftwm, Spectrwm, xmonad, bspwm, dwm, i3wm, etc. For those one can try to autostart stuff by adding systemd targets, depending on graphical enviroment (Gnome, etc.), which is also run by systemd. Commented Aug 28, 2024 at 21:21
  • Good answer, but please don't add details/clarifications in a comment. Instead, update your answer so it stands well by itself Commented Aug 29, 2024 at 7:07
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
1
  • 3
    This execute on bash initialize, not in user login, by example using gdm. Commented May 30, 2020 at 0:31
-1

Just a data point since the question is tagged Ubuntu.

Under Ubuntu 20.04 and 22.04, the only option to autoload of a script/command at each login is (that worked for me) to add it in

~/.bash_profile

As stated in the comments, adding it to ~/.profile doesn't seem to invoke the script. Adding it to /etc/profile.d/ is not per login.

-3

To execute a shell script on login in Ubuntu 11.10, you can add your script to the ~/.bashrc file. This file is executed each time a login shell starts up, so any commands or scripts added here will be run whenever you log in to your system. To add your script to ~/.bashrc, Open a terminal and navigate to your home directory by typing: cd ~/ Next, open the .bashrc file in a text editor using the following command: nano .bashrc Once you have the file open, navigate to the end of the file and add the following line: /path/to/your/script.sh Make sure to replace "/path/to/your/script.sh" with the actual path to your script. Also, make sure that your script is executable by running: chmod +x /path/to/your/script.sh Save and close the file, and the next time you log in, your script should be executed automatically. Let me know if it works for you!

1
  • 3
    The ~/.bashrc file is sourced each time an interactive shell is started, no matter if the user has just logged in or not. Commented Apr 19, 2023 at 4:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.