Scheduling Tasks with Cron

Scheduling Tasks with Cron

Cron allows you to schedule commands to run automatically at set intervals.

Edit the Crontab

crontab -e

Crontab Syntax

* * * * * command
| | | | +-- Day of week (0=Sunday, 7=Sunday)
| | | +---- Month (1-12)
| | +------ Day of month (1-31)
| +-------- Hour (0-23)
+---------- Minute (0-59)

Examples

# Run a backup script every day at 2 AM
0 2 * * * /root/backup.sh

# Restart nginx every Sunday at midnight
0 0 * * 0 systemctl restart nginx

# Run a script every 5 minutes
*/5 * * * * /home/user/check.sh

View Current Crontab

crontab -l

System-Wide Cron Jobs

Place scripts in /etc/cron.daily/, /etc/cron.weekly/, or /etc/cron.monthly/ for automatic execution.

  • 0 用戶發現這個有用
這篇文章有幫助嗎?

相關文章

Managing Users and Groups

Managing Users and Groups on Linux Proper user management is essential for server security. Avoid...

Managing Services with systemctl

Managing Services with systemctl systemctl is the standard tool for managing services on modern...

Monitoring Server Resources

Monitoring Server Resources Keeping an eye on your server's resource usage helps you identify...

Installing and Configuring Nginx

Installing and Configuring Nginx Nginx is a high-performance web server and reverse proxy widely...

Installing and Configuring Apache

Installing and Configuring Apache Apache is the world's most widely deployed web server, known...