crontab is a commonly used scheduling tool in Linux systems. Let’s go over how to use it:
1. Editing directly with the crontab command
The cron service provides the crontab command for setting up cron jobs. Here are some parameters and their descriptions:
crontab -u // Set up cron service for a specific user, typically required when the root user executes this command
crontab -l // List detailed contents of a user’s cron service
crontab -r // Delete a user’s cron service
crontab -e // Edit a user’s cron service
You can use
#crontab -e
to open and edit, as shown below
2. Basic Format
*銆€銆€*銆€銆€*銆€銆€*銆€銆€*銆€銆€command
Minute銆€Hour銆€Day銆€Month銆€Week銆€Command
Column 1 represents minutes 1锝?9 (use * or */1 for every minute)
Column 2 represents hours 1锝?3 (0 means 0 o’clock)
Column 3 represents day of month 1锝?1
Column 4 represents month 1锝?2
Column 5 represents day of week 0锝? (0 means Sunday)
Column 6 is the command to run
Some crontab file examples:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
The example above means restart lighttpd every night at 21:30.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The example above means restart lighttpd at 4:45 on the 1st, 10th, and 22nd of every month.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The example above means restart lighttpd at 1:10 every Saturday and Sunday.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The example above means restart lighttpd every 30 minutes between 18:00 and 23:00 every day.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The example above means restart lighttpd every Saturday at 11:00 pm.
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour between 11 pm and 7 am
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd at 11:00 on the 4th of every month and every Monday through Wednesday
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart