Linux task scheduler (at, crontab)
In Linux, task plans are scheduled to execute certain tasks or periodically execute some tasks.
Task Plan:
Execute a task at a time in the future: at, batch
Periodically execute a task: crontab
After the task is executed, liunx will send an email to the current user.
Email Service: smtp: simple mail transmission protocol
Path in linux:/var/spool/mail/USERNAME
View the MAIL command: yum install mailx to install the mail service
Mail command:
Mail: Enter the interactive mail sending and receiving interface;
Mail-s 'subobject' username @ hostname
Mail-s 'subobject' username @ hostname </PATH/FROM/SOMEFILE
COMMAND | mail-s 'subobject' username @ hostname
At command:
A job running in the future:
Job Queue is supported:
The default value is queue;
Ctrl + d: Submit the task
At [option]... TIME
TIME:
(1) absolute time
HH: MM,
MMDD [CC] YY, MM/DD/[CC] YY, DD. MM. [CC] YY or [CC] YY-MM-DD
Tomorrow
(2) Relative time
Now + # UNIT
Minute, hour, day, week
(3) fuzzy time
Midnight
Noon
Teatime
Common options:
-Q QUEUE: at Job QUEUE;
-F/PATH/FROM/SOMEFILE: reads the job to be run FROM the specified file;
-L: view the list of other running jobs in the Job Queue. It is equivalent to using the atq command;
-C AT_JOB_NUM: view the content of the job to be run;
-D: deletes a specified job, which is equivalent to atrm.
Batch:
The system selects the task to run when the resource is idle;
Crontab: periodic Task Plan
Daemon: crond
There are two types of periodic tasks:
(1) The system cron task does not have the default running user identity, so you need to specify another operator;
/Etc/crontab
Vim command
# Example of job definition:
#. ---------------- Minute (0-59)
# |. ----------- Hour (0-23)
# |. ---------- 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 command to be executed
Seven fields:
First five fields: Time Point
User-name: the user ID used to run the task.
Command to be executed: to run a task
(2) User cron task: a user submits a task. By default, the task runs as a submitter. Therefore, you do not need to specify another operator;
/Var/spool/cron/USERNAME
Vim command; not recommended
Crontab command: Recommended
# Example of job definition:
#. ---------------- Minute (0-59)
# |. ----------- Hour (0-23)
# |. ---------- 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
# |
# ***** Command to be executed
Five time points:
Minute: valid value range: 0-59;
Hour: 0-23
Day: 1-31
Month: 1-12
Weeks: 0-7
Note: It is not recommended that you use both the number of months and the number of weeks;
For example:
6 ****
Time Representation:
*: Each time point within the valid value range of the corresponding time point;
-: A specific continuous time range of 3-7
,: A discrete time point, 3, 5, 7
/#: The amount of time within the effective time range; used to specify the frequency;
1-30/4, */4
5 */3 ***/bin/echo "howdy"
5 7 ** 1-5/bin/echo "howdy"
Crontab command:
Crontab [-u user] [-l |-r |-e]
-Uuser: it is a cron task of the specified target user instead of a cron task. Only root has the permission to manage cron tasks of other users. By default, it manages its own cron tasks;
-L: list: Lists tasks;
-R: remove: removes all tasks;
-E: edit, edit. Open the Default Editor of the Current shell session to edit the cron task table;
Note:
(1) If you do not want to receive the notification email for the task execution result:
COMMAND>/dev/null
COMMAND &>/dev/null
(2) For the crontab file, % has a special function. If % appears in the command, remember to escape it or use single quotes to reference it;
(3) the PATH variable of crontab is not exactly the same as the user variable. Therefore, we recommend that you use the absolute PATH for the task in cron.
/Root/bin/a. sh
Exercise:
1. Every Monday to Saturday 03:20, run the cp command to archive the/etc/directory for another storage, the storage location is/backups/etc-YYYY-MM-DD;
~] # Crontab-l
20 3 ** 1-6/bin/cp/etc-rpf/backups/etc-'file \ % Y-\ % M-\ % d'
2. Run the cp command to back up the/etc/fstab file at 02:30 every Sunday. The storage location is/backup/fstab-YYYY-MM-DD-hh-mm-ss.
~] # Crontab-l
30 2 ** 7/bin/cp/etc/fstab/backup/fstab-'file \ % Y-\ % M-\ % D-\ % H-\ % M- \ % s'
3. at every night, get all the lines starting with S or M in the/proc/meminfo file and append them to the/statistics/meminfo.txt file, add a separator line like =====================;
~] # Crontab-l
00 0 **** echo "====================">/proc/meminfo | egrep "^ S | ^ M"/proc /meminfo>/statistics/meminfo.txt
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: