ubuntu start service on boot

Traditionally, Ubuntu has used various methods for starting services at boot such as upstart or rc.local for scripts. Since Ubuntu 15.10, service startup behavior has been managed using systemctl

Systemctl is strictly for systemd services though. If you want to set up scripts or applications to run on startup, you’ll be better off with different tools.

Start Services on Boot with Systemctl

The systemctl utility has various commands for monitoring and controlling the state of systemd. We’ll configure an NGINX server here for demonstration purposes. Replace nginx with the relevant service unit names in the commands shown below.

To set the nginx service to start at boot, use enable like so

systemctl enable nginx

This’ll create a symbolic link between the service unit file and the systemd autostart location (typically /etc/systemd/system). This means the service will automatically start when booting from now on.

If you want the service to start immediately in the current session as well, you can either reboot or use the above command with the now option.

systemctl enable --now nginx

Similarly, if you want to stop the service from auto-starting at boot, you can use the disable command.

systemctl disable nginx

Add Startup Applications

If you have certain apps that you’d like to auto-launch at startup, you can link the binary with the Startup Applications Preferences feature.

Search and open Startup Applications from the Activities overview, or use the following command in the terminal.

gnome-session-properties

Press Add here and enter the full path of the binary in the command section. You can also browse it from here if you want. The name and comment fields are up to preference.

You’ll find the binaries in the /usr/bin or /snap/bin directories. Or you can also use the which command.

For instance, let’s say you want to set Discord to launch at startup. Find the path of the binary with the which command first.

which discord

Then, enter this path in the Command section, fill in the optional fields, and press Add.

Set Up CronJobs

Cronjobs are used to automatically run programs or scripts at scheduled times. In our case, we can use it to run scripts at startup. The edit option is used to edit the crontab file.

crontab -e

This’ll open the crontab file for the current user. You can add new cronjobs to the end of this file.

You can also prefix sudo to open the root account’s crontab file but in most cases, you should create cronjobs for specific users rather than root.

sudo crontab -e

Now, to run a job at startup, you can use the @reboot shortcut in the following format.

@reboot [command or script path]

For instance, the following command runs the date command and appends the output to the mylog.txt file. This is a common way to log system boot dates and times in Ubuntu.

@reboot date >> ~/mylog.txt

If you want to run a script instead, specify the path of the script like so 

@reboot /home/anup/cleanup.sh

Once you’ve added the cronjobs you want, save the changes and exit the editor. Then, you can list the current cronjobs for verification.

crontab -l
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.