16

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?

9
  • .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. Commented Jun 20, 2011 at 0:19
  • Posting your Ubuntu version might help. In Ubuntu 10.10 .bashrc "works for me (TM)."
    – Vlueboy
    Commented Jun 20, 2011 at 3:39
  • Do you mean when you log in to the GUI, or to the shell?
    – Flimzy
    Commented Jun 20, 2011 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
    Commented Jun 20, 2011 at 4:24
  • I'm using Ubuntu 11.04, as I mentioned in the original question. Commented Jun 20, 2011 at 15:06

6 Answers 6

13

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

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

That should do the trick ;)

6
  • 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? Commented Jun 20, 2011 at 0:40
  • Startup programs should be registered in /etc/xdg/autostart
    – shiftycow
    Commented Jun 20, 2011 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. Commented Jun 20, 2011 at 15:05
  • 2
    /etc/xdg/autostart appears to be a system-wide file, not specific to a user. Commented Jun 20, 2011 at 15:09
  • For later versions of ubuntu see askubuntu.com/questions/159887/…
    – TooTone
    Commented Oct 1, 2013 at 12:09
9

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

[Desktop Entry]
Type=Application
Name=Autostart Script
Exec=autostart
Icon=system-run
X-GNOME-Autostart-enabled=true

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

#!/bin/bash
# Execute bash script below

Make sure that ~/bin/autostart is executable

5

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

1
  • 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 Commented May 18, 2016 at 16:27
1

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.

-1

Extending @JuanSebastianTotero answer.

Instead of:

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

Try

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

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

-1

Press

Super (or window key)

and type

Startup Application

Choose Add In

Command

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .