Running Bash script at login
I guess you want a terminal to see the output of the script, that’s the only problem; the only hack I’ve ever managed to come up with (at least using gnome-terminal or mate-terminal ) is this; change mate-terminal to gnome-terminal and remove sudo -H , and obviously change the chain of commands to just echo hello world
If you don’t need to output to a TTY there are way nicer solution; probably at least some of the methods you’ve tried worked already, you just couldn’t catch the output since the script is not run in a terminal.
Not on Ubuntu right now, however notify-send ‘hello world’ should be enough; create a text file named, say, script.sh , say, in ~/ ; then add a shebang to the start of the file ( #!/bin/bash ) and the command on the next line; mark the script as executable by running chmod +x ~/script.sh and add an entry to Startup Applications to call it; the command would be simply the path to the script, i.e. ~/script.sh
1 Answer 1
The standard location for a script that must run at login is /etc/profile . It will then run for every user (once) when they log in. The user never gets to see the output of the script, it is logged
If it is only for a specific user, it should be added to .profile in their home directory.
With login I mean when you enter your username and password.
Any errors normally show up in ~/.xsession-errors
If it has to run every time you open a terminal window, it should be added to /etc/bash.bashrc or to .bashrc in the user’s home directory.
At work, I mount a number of network shares when I log in. This is done in .profile in my home directory (it needs only to be done once).
Every time I open a terminal window I get a fortune cookie. This happens because the last line in .bashrc in my home directory contains fortune .
Start script on logon
I understand shell executes commands in either ~/.bash_profile, ~/.bash_login or ~/.profile, whichever is found first. My system has only .profile, yet commands entered there won’t get executed on startup/login. The script I’m talking about is essentially a cron front-end/TO-DO list manager. When user logs in (as in user gets access to his personal files/logs in to his account), the script should execute, checking whether there are any tasks that have failed to remind. If it finds any, zenity prompt will be poped up, asking next input.
how do you test if it doesn’t work? have you tried inserting an echo something. into ~/.profile and executing bash -l ?
3 Answers 3
First create your ~/.bash_login and make it do something simply (like echo a phrase.)
echo "echo Look at me go" > ~/.bash_login
Then use bash -l like @mata said. The -l flag will run the bash as if it were the login shell (to make sure it reads your settings files.)
@LaurAliste, please precisely define login. What commands exactly need to be ran? Is this for mounting filesystems or something? If you need something to be used be ran for every ‘login’ irrespective, of what shell or program is actually performing the login, then you need to look at doing something with PAM.
Okay, I’ve found a solution to my initial problem. It’s dirty one though.
First of all, using ~/.bash_profile , ~/.bash_login , ~/.profile or even /etc/profile.d/ should be preferred methods instead (here’s good reading on the subject). Don’t know why it doesn’t work for me; perhaps it has something to do with my display manager auto logon workaround, just a guess.
Another solution would be adding script shortcuts to global file that gets executed at startup, for instance /etc/rc.local .
Since my script requires xserver and user to be logged on, I created initfile which waits after bootup for xserver and user to be logged on and then executes the script:
#! /bin/bash ### BEGIN INIT INFO # Provides: script_file.sh startup at boot # Required-Start: $all # Required-Stop: # X-Start-Before: # Default-Start: 2 3 4 5 # Default-Stop: # Description: runs '/usr/local/bin/script_file.sh --optional --flags' at startup, when xserver has started and YOURUSERNAME is logged on. ### END INIT INFO case "$1" in start) # Wait until X is running and required user logged in: count=0 until [[ "$(tty)" != "\/dev\/tty1" ]] && [[ "$(who | cut -d' ' -f1 | sort | uniq)" == "YOURUSERNAME" ]]; do sleep 5 let count=$count+1 if [[ "$count" == "10" ]]; then echo "Xserver hasn't started or other error occurred. Abort"; exit 1 fi done # Run the script as YOURUSERNAME instead of root: su - YOURUSERNAME -c '/usr/local/bin/script_file.sh --optional --flags' ;; stop) : ;; esac exit 0
This soulution assumes having automatic log-on set up. As mentioned, it really isn’t the way to do it, but works for now.
How to write a shell script that gets executed on login?
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.
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.
5 Answers 5
If you want it to be global, modify
If you want it to be user-specific, modify
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.
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.
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).
/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.
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.
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
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
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.
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!
The ~/.bashrc file is sourced each time an interactive shell is started, no matter if the user has just logged in or not.
Run a script on login using ~/.bash_login
I need to run a script when I login and logout in my Ubuntu. I tried to put the script in my ~/.bash_login but it didn’t work. Is there a better location where I can run my script? My script is located in /home/gsd/script/login.sh and it’s executable. edit: my script runs when i type: /home/gsd/script/login.sh and it set with +x now, i only have: touch /home/gsd/test.txt in the ~/.bash_login to test. the file test.txt is never created edit 2:
gsd@laptop:~$ ll ~/.bash* -rw------- 1 gsd gsd 38639 2012-01-25 17:25 .bash_history -rw-r--r-- 1 gsd gsd 29 2012-01-25 15:22 .bash_login -rw-r--r-- 1 gsd gsd 220 2011-11-03 19:22 .bash_logout -rw-r--r-- 1 gsd gsd 3136 2011-11-04 08:00 .bashrc
It is not necessary to chmod +x .bash_login (set the executable bit on .bash_login). The Bash manual is a bit confusing in this area, but Bash does not eXecute .bash_login like a shell script. It does read the file and then executes the commands within it (You can do something similar by running source ~/.bash_login ).
4 Answers 4
If .bash_profile exists, then Bash will not read .bash_login (or .profile). This annoying feature is described in some versions of the Bash manual, but not all.
.bash_profile and .bash_login are analogous, so I recommend you put your commands in .bash_profile , because it’s is commonly used and .bash_login is relatively unknown. Also consider putting your commands in .bashrc instead of .bash_profile . The manual describes difference between «interactive non-login shell» and «interactive login shell», so be sure to read that section.
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.
Also see this question on superuser, and this Bash howto (Don’t be deterred by the freeunix.dyndns.org:8088 address— it’s still a good quality manual and I have used it for years).
Update, since you say you don’t have a .bash_profile.
It sounds like you are not using what’s called a «interactive non-login shell» (See the Bash manual for a detailed description).
To test this, add something like the following each file: .bashrc , .bash_profile and .bash_login .
echo "DEBUG: I am .bashrc" echo "DEBUG: I am .bash_profile"
How to run a script after user login authentication in linux
I have a script which I want to run just after user log-in authentication. To accomplish this, I added the script name in /etc/rc5.d/S##rc.local file. But later i got to know that, anything that is added in rc.local file gets executed in boot time of the system not after the login authentication. Can anyone tell me how to run the script after user login authentication?
Are you doing this for convenience or for security? I.e. do you just want to set up your login environment (add aliases, set variables, etc), or do you want your script to run after each login without allowing users to remove it?
3 Answers 3
You can add your script to /etc/profile.d folder.
More reading about this here and here.
Basically, you should give your script the extension .sh as all these files are executed in a loop after user logs on.
@user976754: I am glad it helped you. Please mark my answer as your accepted answer for this question 🙂
It looks like you need to have root permissions to create files under /etc/profile.d How then does the file get executed when the user logs in? As the file will be owned by root.
@ams Requiring root permissions for creating script file does not imply it has to be run as root. Files in /etc are always considered affecting a whole system which is implying affection on all users of that system. You definitely don’t want to have anyone writing scripts to be run as you log on with «anyone» including user accounts used to run network services like webserver, only.