How to run scripts on start up?

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.


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.

For 14.04 and older

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

start on startup
task
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).


You can add commands to /etc/rc.local:

sudo nano /etc/rc.local

This executes the commands as root.

To execute commands as a specific user, use sudo -i -u (-i to also run the login shell). For example, to establish a persistent SSH tunnel, where myhost is definde in johndoes ~/.ssh/config file:

sudo -i -u johndoe autossh -nNT -L 1234:localhost:1234 myhost

Note that if /etc/rc.local did not exist (as is the case on Ubuntu since 16.04), you need to add a shebang line at the top (e.g. #!/bin/bash), and ensure the file is executable:

sudo chmod a+x /etc/rc.local