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 nano /etc/rc.local
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