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.
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 0Ensure the file has execute permission and, on systemd‑based distributions, enable the compatibility service:
sudo systemctl enable rc-localsystemd 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.targetReload 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.serviceinit.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 defaultsCron @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 startEach 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
