Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was wondering how to run scripts when my computer starts up so I don't have to run scripts manually that I always run?

share|improve this question
If someone could also show both WHEN and WHERE that would be awesome. I say this because I know there are at least 2 ways to start a script that will fire before other applications have been started (like X11) – Buttink Aug 4 '10 at 20:20
up vote 83 down vote accepted

Depending on what sort of scripts you need to run.. For services and the like you should use upstart. But for a user script these should be launched as session scripts by gnome! Have a look under System > Preferences > Startup Applications.

On a side note if you need some scripts to be run on terminal login you can add them to the .bash_login file in your home directory.

A simple command (one which doesn't need to remain running) could use an Upstart job like:

start on startup
exec /path/to/command

Save this in a .conf file in /etc/init (if you need it to run as root when the system boots up), or in ~/.config/upstart (if you need it to run as your user when you log in).

share|improve this answer
Considering how SO and StackExchange runs, could you give an example of an upstart script and where it would be placed? That would make this a much better answer. Your link says it's not being maintained and to look at the upstart cookbook, which is huuuge. I don't have too much of an idea where to start. – Ehtesh Choudhury Feb 4 '13 at 4:03
What if i need to run the command as root? – dopatraman Jul 15 '15 at 1:58
@dopatraman The answer states that all processes with this are run as root. – cybermonkey Oct 24 '15 at 11:43
Please update this answer to explain what to do on systems running systemd rather than upstart (Ubuntu 15.04+). – Paranoid Panda Jan 9 at 18:52

One approach is to add an @reboot cron task:

  1. Running crontab -e will allow you to edit your cron.
  2. Adding a line like this to it:

    @reboot /path/to/script

    will execute that script once your computer boots up.

share|improve this answer
The @reboot keyword is a nice tip because it is not widely known. – jathanism Aug 11 '10 at 13:39
Nice. Any idea exactly when this triggers? – Oli Feb 2 '11 at 13:54
So... this wouldn't run if I lost power and the PC turned again when power is restored? – Mike Wills Jun 21 '12 at 19:27
@siamii: man 5 crontab says that @reboot is executed on startup (when cron daemon is started). – J.F. Sebastian Mar 29 '13 at 16:04
This is awesome. So far this seems better than rc.local since the system seems more setup by this point (PATH, etc). It is odd that it is so hard to call something after system startup.. – Karthik T Oct 10 '13 at 7:17

How about adding the command to /etc/rc.local? you'll have to use sudo access though to edit this file.

sudo nano /etc/rc.local
share|improve this answer
This most directly answers the question: how to simply execute some scripts when your system boots. upstart does a more complex task: starts daemon processes. – Dogweather Aug 7 '13 at 19:01
Do I need to put the path of my script or it also needs the sh before the path? – Ed Pichler Apr 10 '15 at 21:31
So upstart starts daemon processes while /etc/rc.local starts bash scripts? – Donato May 10 '15 at 18:51

There are different ways to automatically run commands:

  1. The upstart system will execute all scripts from which it finds a configuration in directory /etc/init. These scripts will run during system startup (or in response to certain events, e.g., a shutdown request) and so are the place to run commands that do not interact with the user; all servers are started using this mechanism.

    You can find a readable introduction to at: the man pages man 5 init and man 8 init give you the full details.

  2. A shell script named .gnomerc in your home directory is automatically sourced each time you log in to a GNOME session. You can put arbitrary commands in there; environment variables that you set in this script will be seen by any program that you run in your session.

    Note that the session does not start until the .gnomerc script is finished; therefore, if you want to autostart some long-running program, you need to append & to the program invocation, in order to detach it from the running shell.

  3. The menu option System -> Preferences -> Startup Applications allows you to define what applications should be started when your graphical session starts (Ubuntu predefines quite some), and add or remove them to your taste. This has almost the same purpose and scope of the .gnomerc script, except you don't need to know sh syntax (but neither can you use any sh programming construct).

share|improve this answer
3) "This has almost the same purpose and scope of the .gnomerc script", except .gnomerc apparently runs before loading Unity, and Startup Applications apparently runs after loading Unity. I had to run a program that sits on Unity's menu bar and it made a huge difference in this case! – That Brazilian Guy Jan 23 '13 at 16:13
@ruda.almeida Thanks for pointing that out. The answer was written in the pre-Unity days. – Riccardo Murri Jan 23 '13 at 16:48

For simple things you can add a command in System->Preferences->Sessions pointing to the location of your script.

Alternatively you can add it to /etc/init.d/rc.local or make an upstart job if it's a more low level stuff.

Take a look at for more info

share|improve this answer
  • This location contains startup application list.
  • .desktop file can be put here which will be executed on startup.

Sample example for .desktop file:

Putting following .desktop file in $HOME/.config/autostart and given chmod +x:

[Desktop Entry]
Name=Startup Script

Here "</path/to/script>" is replaced with path to your
(usually recommended to /usr/local/bin so-that can be executed by directly command say myscript replaced with "</path/to/script>").

Sample example of

<commands to be executed>

Result: .desktop file will be launched from $HOME/.config/autostart which execute script by Exec=

Hence, You can run your desired shell script at startup!

share|improve this answer

You should use upstart for this. Upstart is used for Ubuntu processes that are automatically started. It is an enhanced solution like the old System-V init.d scripts. It also allows you to put in prerequisites to the start of your script (i.e. do you need the network running? etc.)

share|improve this answer

To run a (short-lived)1 command at startup using systemd, you can use a systemd unit of type OneShot. For example, create /etc/systemd/system/foo.service containing:

Description=Job that runs your user script



Then run:

sudo systemctl daemon-reload

This is not necessary, since systemd will pick up the new unit on restart. You need to do daemon-reload if you plan to use systemctl on the new unit before restarting.

Essentially, this is just converting a typical Upstart job to a systemd one (see Systemd for Upstart users).


1As opposed to long-lived daemons.

share|improve this answer

protected by Community Jan 14 '15 at 19:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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