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?

Mike Rowave
  • 1,995

6 Answers6

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 ;)

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

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.

studiohack
  • 13,477
-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.

Vuong Tran
  • 101
  • 3
-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.