In Linux, tasks can be configured to run automatically when the specified time period, specified date, or average system load is lower than the specified quantity.
Crontab is a program that conveniently executes a task in a scheduled (Cyclic) manner on unix/linux systems.
The cron service is a daemon that schedules repeated tasks based on the combination of time, date, month, and week. The linux cron service reads all the content under/var/spool/cron,/etc/crontab,/etc/cron. d every minute.
The crond service is usually stored in/etc/init. d/crond, so that the crond service can be automatically started after the system starts.
Linux users use the crontab command to configure cron tasks.
Crontab contains cron. d, cron. daily, cron. weekly, cron. monthly, cron. hourly five directories and crontab files under the/etc directory.
Cron. d is a task automatically and periodically required by the system, but it is not executed by hour, by day, by week, by month, so it is placed under this directory.
If it is executed by hour, by day, by week, or by month, it can be placed under the corresponding directory.
Cron. hourly is a task executed every hour.
Cron. daily is a daily task.
Cron. weekly is a task executed once a week.
Cron. monthly is a task executed once a month.
How to Use Crontab in Linux
Crontab command format:
Crontab [-u user] file
Crontab [-u user] {-e |-l |-r}
-U Parameter Details:
If this option is used, the crontab file of a specific user is modified. If this option is not specified, crontab is the crontab of the current user by default, that is, the crontab file of the user who runs the crontab command will be modified.
Note: If you run the su command and then run the crontab command, it may be confusing. Therefore, when you run the su command, it is best to use the-u option to specify the user's crontab file.
Crontab usage:
Crontab [-u user] file-replace the current crontab with the specified file
Crontab [-u user]-l-list the current crontab of a user
Crontab [-u user]-e-edit the current crontab
Crontab [-u user]-r-delete a user's crontab
NOTE: If crontab-r is used, all scheduled tasks are deleted (Be careful)
Crontab is used to read a crontab file. The main configuration file of the system is crontab. Each row in the/etc/crontab file represents a task. The crontab file has six fields.
The format of the crontab file is m h d m d cmd.
Each field is separated by a space or a Tab key. The first five fields are integers or *. The basic format is as follows (the value range in the brackets ):
# Use the hash sign to prefix a comment
# + ------ Minute (0-59)
# | + ----- Hour (0-23)
# | + ---- Day of month (1-31)
# | + --- Month (1-12)
# | + -- Day of week (0-7) (Sunday = 0 or 7)
# |
F1 f2 f3 f4 f5 command
Hour, day, month, and week commands
F1-minute, any integer between 0 and 59
F2-hour, any integer between 0 and 23
F3-date, any integer between 1 and 31 (if a month is specified, it must be the valid date of the month)
F4-month, any integer from 1 to 12 (or the month can be abbreviated as jan, feb, etc)
F5-week, any integer between 0 and 7. Here 0 or 7 represents Sunday (or use the abbreviation of the week, such as sun or mon)
Command-indicates the task to be executed (you can run linux system commands, or you can execute linux script commands that you have compiled yourself .)
* Function:
An asterisk (*) can be used to represent all valid values. For example, the asterisk in the month value indicates that the command is executed every month after other constraints are met.
When f1 is *, the command is executed every minute, when f2 is *, the task is executed every hour, and so on.
The short-term (-) between integers:
When f1 is a-B, it indicates that execution is performed from the minute a to the minute B. When f2 is a-B, it indicates that execution is performed from the hour a to the hour B, other analogy
The forward slash (/) between integers can be used to specify the Interval Frequency:
When f1 is */n, the execution is performed every n minutes. If f2 is */n, the execution is performed every n hours.
The function of Comma (,) between integers:
When f1 is a, B, c ,... A, B, c ,... Execute in minutes. f2 is a, B, c ,... A, B, c... Execute in hours, and so on
Common examples of crontab:
# Run/bin/ls at every morning:
0 7 ***/bin/ls
#06:10 every morning
10 6 **** date
# Every two hours
0 */2 * date
# Every two hours from PM to am, am
0 23-7/2, 8 **** date
# Am on the 4th day of each month and from Monday to Wednesday of each week
0 11 4 * mon-wed date
# A.m. of July
0 4 1 jan * date
Crontab is often used by WEB server administrators in linux:
# Execute/usr/bin/backup every three hours from to every day within January 1, December:
0 6-12/3*12 */usr/bin/backup
# Restart apache at every night.
30 21 ***/etc/init. d/apache2 restart
# Restart apache on 4: 45 on the 1st, 10th, and 22nd of every month
45 4, 10, 22 **/etc/init. d/apache2 restart
# Restart apache every Saturday and Sunday.
10 1 ** 6, 0/etc/init. d/apache2 restart
# Restart apache every 30 minutes from to every day.
0, 30 18-23 ***/etc/init. d/apache2 restart
# Restart apache at every Saturday.
0 23 ** 6/etc/init. d/apache2 restart
# Restart apache every hour
**/1 ***/etc/init. d/apache2 restart
# Restart apache every hour between PM and PM
* 23-7/1 ***/etc/init. d/apache2 restart
# Restart apache from every Monday to every Wednesday on the 4th of each month
0 11 4 * mon-wed/etc/init. d/apache2 restart
# Restart apache at on January 1, January 1
0 4 1 jan */etc/init. d/apache2 restart
Use crontab in Linux to create scheduled tasks
Routine scheduling of crontab in Linux
Linux crontab does not run troubleshooting
Ubuntu uses crontab for scheduled tasks
Linux scheduled task (at batch crontab anacron)