How to run scripts on start up?

Solution 1:

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.

Solution 2:

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

Solution 3:

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

Solution 4:

For 15.04 and later:

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:

[Unit]
Description=Job that runs your user script

[Service]
ExecStart=/some/command
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then run:

sudo systemctl daemon-reload
sudo systemctl enable foo.service

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

You can run multiple commands from the same service file, using multiple ExecStart lines:

[Service]
ExecStart=/some/command
ExecStart=/another/command some args
ExecStart=-/a/third/command ignore failure

The command must always be given with the full path. If any command fails, the rest aren't run. A - before the path tells systemd to ignore a non-zero exit status (instead of considering it a failure).

Relevant:

  • Arch Wiki entry on systemd
  • man 5 systemd.service

For user sessions, you can create the systemd unit in ~/.config/systemd instead. This should work with 16.04 onwards, but not earlier releases of Ubuntu with systemd (since those still used Upstart for user sessions). User session units can be controlled with the same commands as with system services, but with the --user option added:

systemctl --user daemon-reload
systemctl --user status foo.service

Shell syntax

Note that, unlike Upstart, systemd doesn't run the Exec* commands through a shell. It performs some limited variable expansion and multiple command (separated by ;) itself, but that's about it as far as shell-like syntax goes. For anything more complicated, say redirection or pipes, wrap your command in sh -c '...' or bash -c '...'.


1As opposed to long-lived daemons.