How to run scripts when rebooted/startup on Ubuntu 18.04

Way 1: crontab

Register to cron the script.

crontab -e
@reboot /path/to/script

Way 2: /etc/rc.local

On ubuntu 18.04, /etc/rc.local file automatically run when startup.

Edit the file with nano or other editor you like with sudo.

sudo nano /etc/rc.local

startup – How to run scripts on start up? – Ask Ubuntu

How to run command with sudo

In the case of that you have to run command on startup and that command require sudo privilege, do like following.

#!/bin/sh -e
[YOUR COMMANDS TO RUN ON STARTUP WITH SUDO]
exit 0

If you’d like to run following on startup,

sudo mkswap /dev/sda && sudo swapon -p 32767 /dev/sda

Write following to /etc/rc.local. (make sure there is no sudo in front of each command.)

#!/bin/sh -e
mkswap /dev/sda && swapon -p 32767 /dev/sda
exit 0

And make sure the rc.local file is executable.

sudo chmod +x /etc/rc.local

scripts – Run terminal sudo command at startup – Ask Ubuntu



Articles maybe related

Mount shared folder on Windows 10 on Ubuntu 16.04
Mount shared folder on Windows 10 on Ubuntu 18.04


Close Menu