Operations 3 min read

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.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Linux Service Startup: Crontab @reboot vs Systemd Service

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.sh

This 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 -e

Then add the following line:

@reboot /root/script/restart.sh

After 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.sh

3. 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.target

Enable the service:

$ systemctl daemon-reload
$ systemctl enable restart.service

Now 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/

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.

AutomationLinuxcrontabsystemdstartup script
MaGe Linux Operations
Written by

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.

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.