How to Run Scripts on Start up in Ubuntu

Sometimes you may need to add scripts to start up or run commands on start up. There are various ways to run scripts and programs on start up in Ubuntu. In this article, we will look at each of these cases one by one.


How to Run Scripts on Start up in Ubuntu

There are two ways to run scripts on start up in Ubuntu – using rc.local file and using crontab.


Using rc.local file

When ubuntu system boots, it will automatically execute the scripts mentioned in /etc/rc.local file. Open terminal and run the following command to make rc.local file an executable.

$ sudo chmod +x /etc/rc.local

Now open the file in a text editor.

$ sudo vi /etc/rc.local

Let us say you want to run a script at /home/ubuntu/script.sh on startup. Then add the following line to rc.local file.

$ sudo /home/ubuntu/script.sh &

Please note, you need to provide the full path of the command or script that you want to run on startup. Save and exit the file.

For Redhat, CentOS and Fedora systems, use /etc/rc.d/rc.local file instead of /etc/rc.local.

That’s it. Now your script will automatically start running at boot.

Also read : How to Install HAProxy in Ubuntu


Using crontab

You can also create a cron job to be run at reboot. Open crontab file with the following command.

$ crontab -e

Add the following line to crontab file.

@reboot sudo /home/ubuntu/script.sh &

@reboot specifies that the script needs to be run at start up. You can use this method for almost every Linux distribution.

Also read : How to Import CSV in Python

That’s it. As you can see it is very easy to add script to start up in Ubuntu.