How to Run a Script at Startup on Linux

bash script cpynet

“Running a script at startup in Linux is useful in various scenarios, such as automatically starting server applications. In this article, we will explore different methods to run a script at startup.

Creating a Simple Script

First, let’s create a simple script that will run at startup:

#!/bin/sh
echo "Last reboot time: $(date)" > /etc/motd

This script updates the Message of the Day (MOTD) with the system’s last reboot time. Users will see this message when they log in for the first time.

After saving the script, make it executable:

$ chmod +x reboot_message.sh

Running the Script at Startup Using cron

One of the easiest methods is using cron. For this, we need to edit the crontab file:

$ crontab -e

Add the following line to run the script at startup using the @reboot expression:

@reboot sh /home/ec2-user/reboot_message.sh

While this method is fast and simple, not every version of cron supports the @reboot expression.

Running the Script at Startup Using rc.local

Another method is using the /etc/rc.d/rc.local file. Since this file runs at startup, we can add a line to call our script:

sh /home/ec2-user/reboot_message.sh

For this to work, we need to ensure that the rc.local file is executable:

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

Running the Script at Startup Using init.d

In the /etc/init.d folder, you can find lifecycle scripts for system-managed services. Here, we can create an LSB-compliant script to start our script:

#! /bin/sh
# chkconfig: 345 99 10
case "$1" in
  start)
    # Runs our script
    sudo sh /home/ec2-user/reboot_message.sh
    ;;
  *)
    ;;
esac
exit 0

This script will run when called with the start argument. The chkconfig line determines the runlevel and priority of the script.

After placing the script in the init.d folder, we need to register it to run at startup:

$ chkconfig --add service_wrapper.sh

Since chkconfig is not available on Debian-based systems, you can use the update-rc.d command:

$ update-rc.d service_wrapper.sh defaults

Running the Script at Startup Using systemd

Finally, let’s explore using systemd to run the script at startup. To do this, we need to create a unit file under /etc/systemd/system:

[Unit]
Description=Reboot message systemd service.

[Service]
Type=simple
ExecStart=/bin/bash /home/ec2-user/reboot_message.sh

[Install]
WantedBy=multi-user.target

This file consists of different sections:

  • Unit – Contains general metadata.
  • Service – Describes the service’s behavior and the command to start it.
  • Install – Ensures the service is started at boot and manages dependencies.

After setting the file permissions to 644, let’s enable the service:

$ chmod 644 /etc/systemd/system/reboot_message.service
$ systemctl enable reboot_message.service

While many distributions support systemd, it might not be available on all systems.

Conclusion

In this article, we examined different methods for running a script at startup in Linux. While systemd and cron are typically preferred when available, rc.local and init.d can serve as backup options.

Previous Article

Nginx: Common Errors and Solutions

Next Article

How to Install Node.js and npm on Linux

Subscribe to our Newsletter! 📬

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨