Automate Linux Service Startup: rc.local, Crontab @reboot, and Systemd
This guide shows three reliable ways to run custom scripts automatically after a Linux machine boots—by editing /etc/rc.local, using a crontab @reboot entry, or creating a dedicated systemd service—along with example commands and advanced timing options.
Introduction
When a server loses power and restarts, manually launching many services can be tedious; configuring automatic startup scripts ensures critical processes resume without intervention.
Method 1: Using /etc/rc.local
cat /etc/rc.local
bash /root/script/restart.shPlacing the script call in /etc/rc.local works but is considered less elegant.
Method 2: Using Crontab @reboot
crontab -e @reboot /root/script/restart.shThe @reboot directive runs the specified command each time the host boots.
Run the script after a delay (e.g., 5 minutes):
# Run script 5 minutes after boot
@reboot sleep 300 && /home/wwwjobs/clean-static-cache.shMethod 3: Using a Systemd Service
# /lib/systemd/system/restart.service
[Unit]
Description=restart
After=default.target
[Service]
ExecStart=/root/script/restart.sh
[Install]
WantedBy=default.target systemctl daemon-reload
systemctl enable restart.serviceAfter enabling, the service executes the script automatically on each boot.
Reference Documents
https://www.google.com
https://tinyurl.com/6ryafefw
https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/
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.
