Automate Linux Service Startup: Crontab @reboot vs Systemd Service
Learn how to replace manual rc.local startup scripts with elegant solutions using Crontab's @reboot feature and Systemd services, including step‑by‑step commands and advanced scheduling tricks for reliable automatic execution after a reboot.
1. Introduction
After a power outage, many services need to be restarted manually; creating a startup script and adding it to /etc/rc.local is a common but inelegant solution.
$ cat /etc/rc.local
bash /root/script/restart.shThis works but lacks sophistication. Below are two better approaches.
2. Using Crontab
Crontab supports the @reboot directive to run commands after the system boots. First, edit the crontab:
$ crontab -eThen add the following line:
@reboot /root/script/restart.shAfter saving, the script will automatically execute on reboot. Additional advanced usage includes delayed execution:
# Run the script 5 minutes after boot
@reboot sleep 300 && /home/wwwjobs/clean-static-cache.sh3. Using Systemd
Create a Systemd service file named restart.service:
$ vim /lib/systemd/system/restart.service
[Unit]
Description=restart
After=default.target
[Service]
ExecStart=/root/script/restart.sh
[Install]
WantedBy=default.targetEnable the service:
$ systemctl daemon-reload
$ systemctl enable restart.serviceNow the associated script will start automatically on boot.
4. References
https://www.google.com
https://tinyurl.com/6ryafefw
https://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
