Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
share|improve this question
    
does .bash_login has the right permissions? what does ls -l .bash_login returns? –  santiagozky Jan 25 '12 at 14:48
    
Do you also have a ~/.bash_profile? –  enzotib Jan 25 '12 at 14:52
    
-rw-r--r-- 1 gsd gsd 313 2012-01-22 11:56 .bash_login –  Gino Sullivan Jan 25 '12 at 15:36
1  
chmod +x .bash_login –  Zoke Jan 25 '12 at 16:59
1  
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). –  Stefan Lasiewski Jan 25 '12 at 19:31
show 7 more comments

3 Answers

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

The GNU Bash Reference Manual version 4.1: Bash Startup Files says:

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"

Then log out and log in again. When you log in, I bet you will only see the phrase "DEBUG: I am .bashrc" but not "I am .bash_profile". If so, it means you are a "interactive non-login shell", which simply means that Bash will call .bashrc but not .bash_profile. For information why these dotfiles are the way they are, see @Andrejs Cainikovs's post below and http://mywiki.wooledge.org/DotFiles

share|improve this answer
    
i updated my question, i don't have a bash_profile –  Gino Sullivan Jan 26 '12 at 13:15
    
Ok, because earlier you said "yes i have a bash profile". –  Stefan Lasiewski Jan 26 '12 at 18:07
    
yes, i had it then removed it but same results –  Gino Sullivan Feb 11 '12 at 16:57
2  
This answer solves the problem, I think it's worth accepting it. –  Marius Butuc Mar 26 '12 at 17:23
    
Also note that these bash files are only run when logging into a shell (through Terminal, ssh, etc.), not when logging into the GUI! –  mivk Oct 18 '12 at 8:19
show 1 more comment

Make sure the script you want to run is executable. Run chmod +x scriptname to do that. Also make sure the script starts with the correct shebang (#!/bin/bash for shell scripts). Lastly, use all full paths when executing it, e.g., /usr/bin/echo instead of echo just incase the environment that your script will execute in is not identical to the one you are used to.

share|improve this answer
add comment

Bash only looks for .bash_login or .profile files if it is executed as interactive login shell. When it is executed as interactive non-login shell it reads .bashrc.
Commonly it is the second case, i.e when you run gnome-terminal bash is run as non-login shell.

This clearly states that if you are booting into Gnome, .bash_login will not be executed. But if you lower runlevel to boot directly to bash, the same file will be executed upon succesfull login.
I assume .bash_login will be executed in case of remote SSH connections as well.

Partially ripped from here.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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