How to automatically execute a shell script when logging into Ubuntu

15

5

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?

Mike Rowave

Posted 2011-06-20T00:13:34.300

Reputation: 1 835

http://askubuntu.com/questions/48321/how-do-i-start-applications-automatically-on-login – Ciro Santilli 新疆改造中心法轮功六四事件 – 2015-11-23T18:58:49.130

.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 – 2011-06-20T00:19:56.837

Do you mean when you log in to the GUI, or to the shell? – Flimzy – 2011-06-20T04:03:16.677

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 – 2011-06-20T04:24:32.400

@Flimzy - I mean when I log into the GNOME GUI, not the text-based shell. – Mike Rowave – 2011-06-20T15:07:20.343

Posting your Ubuntu version might help. In Ubuntu 10.10 .bashrc "works for me (TM)." – Vlueboy – 2011-06-20T03:39:49.100

I'm using Ubuntu 11.04, as I mentioned in the original question. – Mike Rowave – 2011-06-20T15:06:37.707

Oops. Sorry. You're not far off from my setup and I've also run into this. You can test putting an alias command alias mydir="ls -l" and logging out and back in. If mydir doesn't work, keep trying it with the "usual suspect" bash profile files that we've mentioned. Then, add your shell script to that file. Back in Solaris 2001 I only had .profile – Vlueboy – 2011-06-21T17:36:21.547

addition to @Vlueboy: For me, .bashrc runs whenever I open a terminal window (12.04) – Brad Hekman – 2012-07-24T13:58:31.117

Answers

11

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

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

That should do the trick ;)

Juan Sebastian Totero

Posted 2011-06-20T00:13:34.300

Reputation: 486

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 – 2011-06-20T00:40:47.827

Startup programs should be registered in /etc/xdg/autostart – shiftycow – 2011-06-20T04:28:25.373

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 – 2011-06-20T15:05:21.297

2/etc/xdg/autostart appears to be a system-wide file, not specific to a user. – Mike Rowave – 2011-06-20T15:09:20.560

For later versions of ubuntu see http://askubuntu.com/questions/159887/where-did-the-startup-applications-preferences-program-go

– TooTone – 2013-10-01T12:09:09.200

bash didn't work for me. Instead sh did. – Rahil Wazir – 2014-04-21T22:05:54.770

8

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

Bert Regelink

Posted 2011-06-20T00:13:34.300

Reputation: 181

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

inLoveWithPython

Posted 2011-06-20T00:13:34.300

Reputation: 161

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à – 2016-05-18T16:27:03.787

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.

Slartibartfast

Posted 2011-06-20T00:13:34.300

Reputation: 6 899

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

Rahil Wazir

Posted 2011-06-20T00:13:34.300

Reputation: 134