Linux periodic task

A one-time task

At a particular time, once cleared after execution

Related command / process

  • at the command
  • atd process

In centos6, the name of the system services: /etc/init.d/atd

The process on the view the system startup:

[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep atd
root      4472     1  0 Sep04 ?        00:00:00 /usr/sbin/atd -f
root      9628  9222  0 18:21 pts/0    00:00:00 grep --color=auto atd

Creating a one-time task

format:

  • at [HH:MM]
  • at [HH:MM] [yyyy-mm-dd]
  • at now + 数字 [minutes |hours | days | weeks]

Example: Creating a scheduled task, the next minute, create a file

[root@ecs-t6-large-2-linux-20190824103606 init.d]# at 18:33
at> touch 123
at> <EOT>
job 3 at Sat Sep 14 18:33:00 2019
[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll
total 44
-rw-r--r--  1 root root     0 Sep 14 18:33 123

Save the scheduled task shortcut key ctrl + d

The commands in the file as a regular task

Format: at the time -f file

Example: Five minutes after the execution /root/test.sh

at now + 5 minutes -f /root/test.sh

Query-time regular tasks on the current system

  • Command 1:
at -l
[root@ecs-t6-large-2-linux-20190824103606 init.d]# at -l
没任何结果说明没有一次性任务
  • Command 2:
atq
[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
没任何结果说明没有一次性任务

[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
5   Sat Sep 14 20:00:00 2019 a root
5  是任务号

According to the task number to delete the specified time task

  • Command 1
atrm [编号]
  • Command 2
at -d

View the details of a one-time task

at -c [任务号]

Time task files created location

/ var / spool / AT / a *
In this path, all the files are in a beginning there

[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll /var/spool/at/
total 8
-rwx------  1 root root 3085 Sep 14 18:37 a00005018ee170
drwx------. 2 root root 4096 Sep 14 18:33 spool

After the task performed, the file disappears

Which shows whether the user can use scheduled tasks in those files

  • /etc/at.deny : The list of users can not use the regular task
  • /etc/at.allow : this list available to users

Periodic task

According to the plan repetitive tasks booking

Related command / process

  • crontab command
  • crond process

In centos6, the period corresponding to the task services are: /etc/init.d/crond

View the system if the process is started

[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep crond
root      4474     1  0 Sep04 ?        00:00:00 /usr/sbin/crond -n
root      9699  9222  0 18:23 pts/0    00:00:00 grep --color=auto crond

Cycle created task file location

/ Var / spool / cron / username

So the existence of periodic tasks can view the system as follows:

[root@ecs-t6-large-2-linux-20190824103606 init.d]# cat  /var/spool/cron/root 
5 11 * * * /tmp/touch_file

cron service configuration file

[root@ecs-t6-large-2-linux-20190824103606 init.d]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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

Note that the above PATH, this may PATH and PATH different systems, which means that when we knock some command line commands These commands can be found according to the PATH system, but if the duty cycle PATH is missing some paths, you lead cycle may fail to run the command

  • Check the PATH system, and (my default is exactly the same)
[root@ecs-t6-large-2-linux-20190824103606 init.d]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin

If there really can not be executed command how to do? I can add absolute path / bin in front of the command / command

Example: Creating a script file

[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat touch_file 
touch a$RANDOM

This script execution

[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash touch_file 
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root     0 Sep 14 18:59 a21698

Add to this script x, allowed executables

[root@ecs-t6-large-2-linux-20190824103606 tmp]# chmod +x touch_file 
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
-rwxr-xr-x 1 root root    15 Sep 14 18:59 touch_file

Perform again:

[root@ecs-t6-large-2-linux-20190824103606 tmp]# ./touch_file 
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root     0 Sep 14 18:59 a21698
-rw-r--r-- 1 root root     0 Sep 14 19:01 a3460

When we switch to a different directory, you can only run the script by adding the absolute path

[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash /tmp/touch_file 

Add to cyclic tasks

View cron service log files

/ Var / log / cron

From this log file can be seen in the history of task execution record

Cron task management plans

  • Editing scheduled tasks: crontab -e [-u username]
  • View scheduled tasks: crontab -l [-u username]
  • To delete a scheduled task: crontab -r [-u username]

Periodic task format

[root@ecs-t6-large-2-linux-20190824103606 init.d]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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


*           *               *                   *                *
分钟      小时            日期                月份            星期
0-59      0-23             1-31              1-12            0-7

0  17  *  *  1- 5       周一到周五的每天17:00
30  8  *  *  1,3,5       星期1,3,5 每天八点半
0  8-18  *  *  *          每天的8-18点
0  12     *  *              每隔三天的12点

Guess you like

Origin www.cnblogs.com/ZhuChangwu/p/11519940.html