There are two scheduled tasks under Linux: Execute only once and periodically.
(1) Execute once and execute once at a specified time in the future
The time formats specified are:
Absolute time: Hh:mm,dd. Mm. Yy,mm/dd/yy
Relative Time: now+ number (Minutes,hours,days,weeks)
Blur Time: Noon (i.e. 12 points), midnight (i.e. 24 points), teatime (i.e. 16 points)
Command format: at [options] [TIME]
Command options:
-m specifies that a message will be sent to the user when the task is completed
-L is the same as the ATQ command
-D is the same as the ATRM command
-V shows when the task will be executed
-C Print Task contents to standard output
-V Display version information
ATD Service must be started
| rhel6 |
Rhel7 |
#service atd restart |
#systemctl restart atd.service |
After 1, 2 minutes, enter the current time to/tmp/1.txt,
[[email protected] ~]# at now+2minutes #回车at> echo `date` >> /tmp/1.txt #回车at> <EOT> #写入完命令后按下crtl+d提交任务job 3 at Thu Mar 8 15:44:00 2018[[email protected] ~]# tail -f /tmp/1.txt 2018年 03月 08日 星期四 15:44:00 CST
2. View current at tasks, at-l
[[email protected] ~]# at now+5minutesat> echo `date` >> /tmp/1.txtat> <EOT>job 4 at Thu Mar 8 15:52:00 2018[[email protected] ~]# at -l4 Thu Mar 8 15:52:00 2018 a root[[email protected] ~]#
3, delete the task, at-d JobID or ATRM JobID
[[email protected] ~]# at now+5minutesat> echo `date` >> /tmp/1.txtat> <EOT>job 5 at Thu Mar 8 15:54:00 2018[[email protected] ~]# at -l5 Thu Mar
4. Automatically perform tasks when the system is idle
[[email protected] ~]# batchat> echo "hello" > /dev/pts/1 at> <EOT>job 6 at Thu Mar 8 15:55:00 2018[[email protected] ~]# hello[[email protected] ~]#
At has two profiles/etc/at.deny and/etc/at.allow
If deny exists alone, all users except the Deny file record can use the AT command
If Allow is present alone, only allowed users can use the AT command
If it exists at the same time, only allow users to use the AT command
(2) Periodic execution of tasks
Recurring tasks over a specified period of time
Command format: crontab [options] [parameters]
Command options:
-u Specifies to set a user's scheduled task, only root can set
-l list user's scheduled Tasks
-R Delete all scheduled tasks for a user
-e Edit all scheduled tasks for a user
Time Format Description:
First position means: minute (0-59)
The second position indicates: hours (0-23)
The third position represents: Date (1-31)
The fourth position means: month (1-12)
The fifth position means: Week (0-7,0 and 7 both represent Sunday)
The sixth position indicates: the command to be executed
Time-Pass representation:
*Represents all points in time at the current location
5 * * * *Starting at 05 minutes per day
,Discrete point-in-time
10,25,50 * * * *Every hour, 10 minutes, 25 minutes, 50 minutes each.
-Continuous point-in-time
30-40 * * * *Every day, 30-40 minutes per hour, per minute.
/How many times per interval within the corresponding value range
1 */2 * * *Run every two hours, 01 minutes
Crond task is divided into user plan and system plan
1, the System planning configuration file in the/etc/crontab file
2, the user plan in/var/spool/cron/username
Crontab permissions issue to/var/adm/cron/next look, file Cron.allow and Cron.deny exist
Use the following:
1. If none of the two files exist, only the root user can use the crontab command.
2, if the Cron.allow exists but Cron.deny does not exist, only the users listed in the Cron.allow file can use the crontab command, if the root user is not inside, then the root user can not use crontab.
3, if the Cron.allow does not exist, Cron.deny exist, only the users listed in the Cron.deny file can not use the crontab command, other users can use.
4. If two files are present, Users who are listed in the Cron.allow file and are not listed in Cron.deny can use crontab, if the same user is in two files, to cron.allow whether the user is in the file, or if there is a user in Cron.allow, you can use the crontab command.
Crond Service must be started
|
rhel6 |
Rhel7 |
#service crond restart |
#systemctl restart crond |
Example:
- 09 minutes per hour to execute
9 * * * *
- 3 to 6 of 10 points, 23 points each week
0 10,23 * * 3-6Or0 10,23 * * 3,4,5,6
- Run every 2 minutes between 10-30 points per hour
10-30/2 * * * *
- Once daily 12:09
9 12 * * *
- Once a week, 3, 12:09.
9 12 * * 3
- Once every month, 9th, 12:09.
9 12 9 * *
- Each year, 6, December, 9th, 12:09, respectively, executed once
9 12 9 6,12 *
Study Questions
* */2 * * *, what does it mean?
0 12 9 * 6, what does it mean?
Answer
- Every hour, every minute.
- At 9th per month and Saturday, 12 points are being executed once (generally less days and weeks, as the probability of satisfying the conditions is lower.) )
Linux Basic planning tasks