How to Execute a Command On Reboot in Linux

Being a UNIX operating system system admin or maybe a general UNIX operating system user, you may be needed to run some commands or scripts at an everyday interval or at a required time. we tend to use crontab to accomplish these tasks & we’ve already mentioned Crontab in our tutorial. but however will we tend to execute a command or script on system startup or when a reboot?

Well, there area unit2ways that we will execute a command or script on system startup or when a boot,

1- using ‘/etc/rc.local’ file

2- using Crontab

Also Read: How to Analyze Real-Time Apache and Nginx Logs using GoAccess

Let’s discuss both these methods one by one.

1- Using ‘/etc/rc.local’ file

This is my go-to methodology once I have to be compelled to execute a command or script on system startup. To execute a command on startup, open the file ‘/etc/rc.local’,

NOTE:- within the latest CentOS version, we would realize this enter ‘/etc/rc.d/rc.local’.

$ sudo vi /etc/rc.local

& add it into the file with full command path, like,


Save file & exit. to induce the complete path of the command, you’ll run the ‘which’ command,

which date

Now the command can execute on every startup or when a bootalsoto feature a script to the file, initial make certain that the script is viable,

$ chmod +x /home/clinerds/

& then edit the rc.local file,

$ sudo vi /etc/rc.local

/bin/sh /home/clinerds/

Save the file & exit, we have a tendency to square measure done. currently let’s see the second technique also.

2- Using Crontab

For this method, we handiest want to create a new crontab activity in our machine. So to create a new crontab activity, run the subsequent command,

$ crontab -e

Then upload the subsequent activity to crontab,

@reboot (sleep 120; /bin/sh /home/clinerds/

So we delivered the activity to run on each reboot with a snooze duration of 120 seconds due to the fact we need our machine to be absolutely up earlier than our script is executed, otherwise, our script may fail to run. We now give up this educational on a way to execute command or script on machine startup or after a reboot. Please do proportion any questions or queries use the comment field below.

Leave a Reply