How do I get a script to execute automatically when I log in? Not when the machine starts up, and not for all users, but only when I (or any specific user with the script) login via the GNOME UI.

From reading elsewhere I thought it was .bash_profile in my home directory, but for me it has no effect. When I manually execute it in a terminal window by typing ~/.bash_profile it works, but it won't run automatically when I log in.

I'm running Ubuntu 11.04. The file permission on my .bash_profile is -rwx------. No .bash_profile existed in my home directory before I created it today.

I seem to remember older versions of Linux having a .profile file for each user, but that doesn't work either.

How is it done? Do I need to configure something else to get the .bash_profile to work? Or does the per-user login script need to be in some other file?

  • .bash_profile, .profile and .bashrc (Which is the one actually used by ubuntu) are loaded each time you open a bash terminal. So actually I'm not sure they will be loaded if you just login into GDM. – Juan Sebastian Totero Jun 20 '11 at 0:19
  • Posting your Ubuntu version might help. In Ubuntu 10.10 .bashrc "works for me (TM)." – Vlueboy Jun 20 '11 at 3:39
  • Do you mean when you log in to the GUI, or to the shell? – Flimzy Jun 20 '11 at 4:03
  • bash loads them when you start a new shell, so they will only get loaded when you open a terminal window or log into a virtual terminal, not when you log into Gnome (GDM). – shiftycow Jun 20 '11 at 4:24
  • I'm using Ubuntu 11.04, as I mentioned in the original question. – Mike Rowave Jun 20 '11 at 15:06

You could simply add the following command into System > Preferences > Startup Applications:

bash /full/path/to/your/script.sh

That should do the trick ;)

  • You know what, I did that before and it didn't work ... except that I didn't write "bash" before the script's path! I'll try it with the bash later. Out of curiosity, when you enter something via the GUI into Startup Applications, in which file is it registered? – Mike Rowave Jun 20 '11 at 0:40
  • Startup programs should be registered in /etc/xdg/autostart – shiftycow Jun 20 '11 at 4:28
  • Adding bash /home/myusername/scriptname to "Startup Applications" worked! Thanks. However this might be specific to GNOME on Ubuntu, that's why I was wondering about where the entries in that "Startup Applications" menu item are registered, as that would be more likely to be similar in other modern versions of Linux. – Mike Rowave Jun 20 '11 at 15:05
  • 2
    /etc/xdg/autostart appears to be a system-wide file, not specific to a user. – Mike Rowave Jun 20 '11 at 15:09
  • For later versions of ubuntu see askubuntu.com/questions/159887/… – TooTone Oct 1 '13 at 12:09

So basically, as nodiscc suggested, create a desktop launcher: ~/.config/autostart/script.desktop with the following contents:

[Desktop Entry]
Name=Autostart Script

Then create the autostart script: ~/bin/autostart with your bash contents:

# Execute bash script below

Make sure that ~/bin/autostart is executable


You can add a line in crontab -

crontab -e

Then add this line to the file that opens up:

@reboot /path/to/your/cool/script

This will run the script at reboot. For more details see man crontab

  • I don't know why, but I can't execute .sh files on "start up applications" , therefore the easyest way to do it now, is cron – Albert Català May 18 '16 at 16:27

Try ~/.xinitrc (some info here: https://wiki.archlinux.org/index.php/Xinitrc ). Remember that anything you start in this script should be started / run in the background, or it could interfere with the X login.



Super (or window key)

and type

Startup Application

Choose Add In


Box, use this command below:

bash "full/path/to/your/file.sh" --autostart

In my case, I'm using small application to swap ctrl vs alt key. You don't need to type command every turn on your computer.

  • Just avoid duplicate guide, and simple to do, thanks anyway. I'll modify it. – Vuong Tran Oct 19 '20 at 1:54

Extending @JuanSebastianTotero answer.

Instead of:

bash /full/path/to/your/script.sh


sh /full/path/to/your/script.sh

bash didn't work for me on Ubuntu 13.04 and 14.04. But sh does.

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.