Operations 9 min read

How to Run Commands Automatically on Linux Boot: rc.local, systemd, init.d, and cron

Learn multiple ways to execute commands or scripts during Linux reboot or startup—including the classic rc.local, modern systemd service units, traditional init.d scripts, and cron @reboot—complete with step‑by‑step instructions, example code, and essential tips for reliable automation.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Run Commands Automatically on Linux Boot: rc.local, systemd, init.d, and cron

rc.local Method

The traditional rc.local file runs custom commands near the end of the boot process. Edit /etc/rc.local, make it executable, and add the desired script or commands.

sudo vi /etc/rc.local
#!/bin/bash
# Print welcome message
echo "欢迎回到 Linux 世界!"
# Start custom service
/home/user/myservice start
exit 0

Ensure the file has execute permission and, on systemd‑based distributions, enable the compatibility service:

sudo systemctl enable rc-local

systemd Service Unit

On modern Linux, systemd manages services. Create a unit file that runs a script once at boot.

sudo vi /etc/systemd/system/mybootscript.service
[Unit]
Description=My Boot Script Service
After=network.target

[Service]
Type=oneshot
ExecStart=/home/user/bootscript.sh

[Install]
WantedBy=multi-user.target

Reload the daemon, enable, and start the service. Use journalctl to view logs.

sudo systemctl daemon-reload
sudo systemctl enable mybootscript.service
sudo systemctl start mybootscript.service
sudo journalctl -u mybootscript.service

init.d Script

For SysVinit‑compatible systems, place a script in /etc/init.d with proper header information.

sudo vi /etc/init.d/myinitdscript
#!/bin/bash
# BEGIN INIT INFO
# Provides: myinitdscript
# Required-Start: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Short-Description: My Init.d Script
# Description: This script is used to perform custom tasks during system startup and shutdown.
# END INIT INFO

case "$1" in
  start)
    echo "Starting myinitdscript"
    /home/user/myscript.sh start
    ;;
  stop)
    echo "Stopping myinitdscript"
    /home/user/myscript.sh stop
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac
exit 0
sudo chmod +x /etc/init.d/myinitdscript
sudo update-rc.d myinitdscript defaults

Cron @reboot

Although cron is mainly for periodic tasks, the @reboot directive can run a script at boot. @reboot root /home/user/cleanup.sh Ensure the cron daemon is running:

sudo service cron status
sudo service cron start

Each method has its own advantages: rc.local is simple but may be disabled; systemd offers fine‑grained control and logging; init.d provides compatibility with older SysVinit systems; and cron @reboot is easy to add alongside other scheduled jobs. Choose the approach that best fits your distribution and automation requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Automationcronstartupsystemdrc.local
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.