How to execute a command or script at reboot or startup

Well-known services in Linux can be added on boot time without any problems, because most of them come with their own script, which is done using ‘chkconfig’ and ‘systemctl’ commands.

For example, to add ‘Apache httpd’ service on boot, run one of the following commands based on your system manager. Similarly you can add any well known service as required at startup.

For Sys V init system:

# chkconfig --level 35 httpd on

For systemd system:

# systemctl enable httpd

Sometimes you may need to add a custom script or command or service at startup/boot. If so, how do you do it?

In this article, let’s understand how to use the following three methods to implement it:

  • Using /etc/rc.d/rc.local file
  • Using crontab file
  • systemd service unit

Method-1: Using /etc/rc.d/rc.local file

The “/etc/rc.local” file is traditionally executed after all normal computer services have been started at the end of the process of switching to a multiuser runlevel.

This method also works on the systemd system, and you need to add the location of your script to the ‘/etc/rc.d/rc.local’ file to run on boot.

Make sure the file has executable permission to run.

# chmod +x /etc/rc.d/rc.local

To understand this in detail, we will create a simple script as shown below, but you can create any script as needed:

# vi /opt/scripts/run-script-on-boot.sh

#!/bin/bash
date > /root/on-boot-output.txt
hostname > /root/on-boot-output.txt

Once the script is ready, set the executable permission as shown below:

# chmod +x /opt/scripts/run-script-on-boot.sh

Finally add the script to the bottom of the file:

# vi /etc/rc.d/rc.local

/opt/scripts/run-script-on-boot.sh

Restart your system to verify this:

# reboot

Method-2: Using crontab file

cron executes scheduled jobs automatically in the backend at a specific time.

This can be easily accomplished using a special string called "@reboot" with cron job.

@reboot is a special string and allows the user to run any command or script at startup (boot time).

This example runs the ‘/opt/scripts/run-script-on-boot.sh’ file on the system restart.

We will use the same script which was created in the above example. To do so, just add the following entry in the crontab file:

# crontab -e

@reboot /opt/scripts/run-script-on-boot.sh

Restart your system to check this:

# reboot

Method-3: Using systemd service unit

This method only works on the systemd system and it is very straightforward.

To do so, you need to create a systemd startup script and place it in the “/etc/systemd/system/” directory.

This is our sample systemd startup unit script:

# vi sample-on-boot-script.service

[Unit]
Description=Run a Custom Script at Startup
After=default.target

[Service]
ExecStart=/opt/scripts/run-script-on-boot.sh

[Install]
WantedBy=default.target

Once you place the unit script in the systemd location, run the following command to update the systemd configuration files and enable the service:

# systemctl daemon-reload
# systemctl enable sample-on-boot-script.service

Restart your system to check this:

# reboot

Bonus Tips:

If you want to run a script in the background, you need to add the trailing ampersand "&" symbol.

/Path/To/My_Script &

If you want to run the command as a different user, use the following format:

su - $USER -c /Path/To/My_Script

Conclusion

This tutorial briefly discusses how to add a custom script or command or service at startup.

Feel free to leave a comment if you have any questions.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

One Comment on “How to execute a command or script at reboot or startup”

  1. I have a question, if what I need os to this script to run after all the services are already started and running, is it the same using ‘After=default.target’ than ‘After=multi-user.target’?

Leave a Reply to ScarUY Cancel reply

Your email address will not be published. Required fields are marked *