Linux timed tasks at and crontab

Source: Internet
Author: User

1. at Disposable Tasks

1.1 Command at Installation

Reads a command from a file or standard input and executes at a later time, only once. atthe normal execution of the process requires a daemonatd.

#安装at Yum install-y at# start daemon Service ATD start #查看是否开机启动 chkconfig--list|grep ATD #设置开机启动 Chkconfig--level 235 ATD on


1.2 Use

If you do not use a pipe | or specify options -f , at the execution will be interactive and you will need to enter the command at the prompt of at:

[Email protected] ~]# at now +2 minutes #执行at并指定执行时刻为现在时间的后两分钟at > Echo Hello World >/root/a.txt #手动输入命令并回车at > <EOT> #ctrl +d End Input Job 2 at 2017-07-24 16:08 #显示任务号及执行时间

Options -l or Command atq Query task

[[email protected] ~]# atq2 2017-07-24 16:21 a root[[email protected] ~]# at-l2 2017-07-24 16:21 a root

After the arrival time, the task is executed, a new file is generated and the output of ECHO is saved.

[email protected] ~]# cat a.txt Hello World


atSpecify the time method:
1) hh:mm Hours: Minutes (the day, if the time has past, then the next day of execution)
2) midnight (Late Night), ( noon noon), teatime (Afternoon tea time, 4 o'clock in the afternoon), today , tomorrow etc.
3) 12 hour clock, time plus am (morning) or pm (PM)
4) Specify the specific execution date mm/dd/yy (month/day/year) or dd.mm.yy (day, month, year).
5) Relative timing method now + n units , now is the moment, n is the number, units is the unit (minutes, hours, days, weeks)


Create a directory like tomorrow 2:20 P.M.

[Email protected]]# at 02:20pm tomorrowat> mkdir/root/temp/xat> <eot>job one at Fri Dec 23 14:20:00 2016


An option -d or command atrm represents a delete task

[Email protected]]# at-d #删除11号任务 (previous example) [[Email protected]]# atq[[email protected]]#


You can use pipelines | or options -f to at get tasks from standard input or files

[[Email protected]~]# cat test.txt echo Hello World >/root/temp/file[[email protected]~] at-f test.txt 5pm +2 daysjob In Sat Dec 17:00:00 2016[[email protected]~]# cat test.txt|at 16:20 12/23/16job at Fri Dec 23 16:20:00 2016

atdWith two files /etc/at.allow and /etc/at.deny to determine which users in the system can use the at Set Timer task, it first checks /etc/at.allow that if the file exists, only the users listed in the file (one per row) can use at, and if not, check the file /etc/at.deny . All users who are not in this file can use at. /etc/at.denyan empty file means that at is available to all users in the system, and if the /etc/at.deny file does not exist, only superuser (root) can use at.

2. crontab

Each user in the system can have their own cron table , atd similar, crond there are two files /etc/cron.allow and /etc/cron.deny used to restrict users to use cron, the rules are atd the same as the two files.


/etc/cron.deny represents a user who cannot use the crontab command

/etc/cron.allow represents a user who can use crontab.

If two files exist at the same time, then/etc/cron.allow is preferred.

If none of the two files exist, only the Superuser can schedule the job.

Each user will generate a crontab file of their own. These files are in the/var/spool/cron directory


Crontab-u Specify a user

Crontab-l List A user's task schedule

Crontab-r Delete a user's task

CRONTAB-E editing a user's tasks


/etc/cron.{ Hourly,daily,weekly,monthly} , the files in the directory define the hourly, daily, weekly, monthly scripts that need to be run, and the exact time to run these tasks is specified in the file /etc/crontab . such as:

[[email protected] spool]# cat /etc/crontabshell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/ binmailto=roothome=/# for details see man 4 crontabs# example of  job definition:# .---------------- minute  (0 - 59) # |  .--------- ---- hour  (0 - 23) # |  |  .---------- day of month   (1 - 31) # |  |  |  .------- month  (1 -  (&NBSP;OR&NBSP;JAN,FEB,MAR,APR&NBSP;...#&NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP):----  day of week  (0 - 6)   (sunday=0 or 7)  or sun,mon,tue,wed , thu,fri,sat# |  |  |  |  |# *  *  *   *  * user-name command to be executed# run-parts01 *  * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts / Etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1  * * root run-parts /etc/cron.monthly The first part represents the minute (0-59),*  means the second part of each minute represents the hour (0-23), *   indicates that the third part of the hour represents the day (1-31),  *  means that the Part IV represents the month (1-12),  *  that the fifth part of the month represents the week (0-6,0 represents Sunday), *   represents the task to be performed in Part VI of the week

The purpose of


Anacron is not to completely replace Cron as a supplement to cron . The task definition for Anacron is in the file /etc/anacrontab :

[[email protected] spool]# cat /etc/anacrontab # /etc/anacrontab:  Configuration file for anacron# see anacron (8)  and anacrontab (5)  for  details. shell=/bin/shpath=/sbin:/bin:/usr/sbin:/usr/binmailto=root# the maximal random delay  Added to the base delay of the jobsrandom_delay=45# the jobs  will be started during the following hours onlystart_hours_range=3-22 #period  in days   delay in minutes   job-identifier    command1       5        cron.daily              nice  run-parts /etc/cron.daily7       25       cron.weekly             nice run-parts /etc/cron.weekly@ monthly 45     cron.monthly             nice run-parts /etc/cron.monthly



This article from "High-rise, look at the end of the Road" blog, please be sure to keep this source http://nxyboy.blog.51cto.com/10511646/1950598

Linux timed tasks at and crontab

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.