Scheduled tasks in Linux
Scheduled tasks in Linux are divided into two types:
1. One-time execution:
Common commands: at and batch, depending on the atd service
2. Periodic execution:
Common commands: crontab, depending on the crond Service
One-time task execution:
Usage:
# At TIME
At>/bin/sync enter the command or script to be executed
At> press Ctrl + d and enter Cry + d to submit the task.
TIME: several formats
# At now + 3 min relative time
# At or absolute time
# At noon, midnight, teatime (pm) or fuzzy time
Man
At Command Options:
-M: After the at operation is completed, the user will be notified by mail that the work has been completed even if no information is output.
-L: lists the task queues currently waiting for execution.
# At-l = # atq
-D: delete unexecuted tasks
# At-dJOB_ID = atrm JOB_ID
-F: Specifies to execute the script in the corresponding directory.
# At-f/PATH/TO/AT_SCRIPT TIME specifies TO execute the script in the corresponding directory
Here/PATH/TO/AT_SCRIPT is called the at Script
Example: at instance
Before changing the ip address of the remote host, you can execute an at task first to avoid the connection failure caused by the new IP address.
# At now 10 min
At> ifconfig eth0 IP # this IP address is the correct IP address currently in use
Send the at task first, and then change the IP address. Even if an error is corrected, the task will be automatically restored in 10 minutes.
Unlike at, batch can be automatically executed when the system is idle without specifying the time. When you plan to execute some batch processing tasks, you may use them to make your own assessment and select the system to run when idle.
Periodic task:
There are two types of periodic tasks:
System periodic task:/etc/crontab
User-class periodic tasks:/var/spool/cron/USERNAME
Definition of system cron:
Method:
1. # vim/etc/crontab: Add a scheduled task to the configuration file
2. # service crond restart the crond service to make the newly added scheduled task take effect.
Description of the seven fields in the/etc/crontab configuration file of the system cron:
# Cat/etc/crontab
# Example of job definition:
#. -------------- Minute (0-59) # Run the first field in minutes.
# |. ------------- Hour (0-23) # Second Field
# |. ---------- Day of month (1-31)
# |. ------- Month (1-12) OR jan, feb, mar, apr...
# |. ---- Day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
# |
# ***** User-name (sixth field) command to be executed (seventh field)
Definition of cron:
Method:
# Crontab-e [-u USERNAME]
This command automatically opens the editor to create a task, modify the original task, delete the original task, and save it. This command is used to edit the file to provide the syntax check function, but not through vim. Similar to mongodo.
The saved cron file is automatically saved in the/var/spool/cron/USERNAME file. This file defines six fields, which are the same as those in the configuration file/etc/crontab, only remove the user-name segment, that is, compared with the system cron, the user cron does not need to specify the user identity
Crontab command options:
-E: Open the default text editor of the current system to define the user cron.
-R: removes the configuration file and deletes all tasks;
(If you delete a specified task, click crontab-e to open the file and delete the corresponding row)
-L: List periodic tasks
-U USERNAME: the Administrator defines crontab as another user and stores the crontab in the directory of the corresponding user name.
E. g. # crontab-e-u user1 (so the root user can manage tasks of other users)
The time segment in the cron configuration file. The following wildcards can be used when the time is specified:
*: All valid values of the corresponding time point, that is, each
E. g. 3 4 *** root cat/etc/fstab run the command cat/etc/fstab as the root user at 04:03 every day
3 4 ** 5 root cat/etc/fstab run every Friday 04:03 (weekly and monthly, usually not used at the same time)
,: Discrete value
E. g., *** root cat/etc/fstab
3rd minutes per hour, 18 minutes ...., That is, once every 5 minutes
/: Defines the frequency within the specified value range
E. g. */3 ***** root cat/etc/fstab is executed every 3 minutes
17 */3 * root cat/etc/fstab is executed every 17th minutes every 3 hours, that is, once every 3 hours-: continuous value:
E. g. 17 8-20/3 *** root cat/etc/fstab run once every 3 hours from to every day
For example, ** is executed twice a week (Tuesday and Friday)
2,328-18/3 ** 1-5 executed every three hours from Monday to Friday,
72 */2 ** root tar-Jcf/backup/etc-'date 'Too many f'''.tar. xz/etc/* backup The/etc directory at 02:07 every 2 days.
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)
This article permanently updates the link address: