Linux Task Scheduler At,cron

Source: Internet
Author: User

Task Scheduling on Linux:

One is sudden, that is, a certain time in the future to perform a task at once

Related commands: At, Batch

One is cyclical, that is, every certain period of time to perform a task

Related commands: crontab


At command: handles tasks that only need to be done once

The AT command must be executed with the support of the ATD service to start the ATD service.

Note:There is a change in the configuration of the service in Centos7, the following command only applies to the previous version of 7, which is being looked up and added shortly.

#开启atd服务 [[email protected] ~]#/etc/init.d/atd restartstopping ATD: [OK]s tarting ATD: [OK]

How at is run:

Use the AT command to schedule the task that will be performed and write the task as text to the/var/spool/at/directory, which waits for the ATD service to be invoked and executed.

Based on security considerations, not all people can use the AT command to schedule tasks. Linux uses the two files of/etc/at.allow and/etc/at.deny to limit the use of at.

1 First look for the/etc/at.allow file, in this file exists in the user can use at command, not in this file user cannot use at command, (even if not in the At.deny file) Note:/etc/at.allow than/etc/ At.allow to Priority

2 If the/etc/at.allow file does not exist, look for the/etc/at.deny file, and if there is a user in the file, you cannot use the AT command, and the user who does not exist in this file can use the AT command.

3 If none of the two files exist, then only root can use the AT command

Syntax for at command: Man at

Support for using job queues:
The default is a queue;

At [-v] [-q queue] [-f file] [-mldbv] timeat [-v] [-q queue]  [-f file] [-mldbv] -t time_argat -c job [job ...] Atq [-v] [-q queue]atrm [-v] job [job ...] Parameter: -q  specifies the queue, which is listed by default queue  -f /path/from/somefile: reads the job to run from the specified file  -m  after the at work is complete, even if no information is output , also notify users by email  -l  list all of the user's at tasks above the current system   equivalent to the ATQ command  -d  cancel a task in the at Schedule   -c at _job_num: Lists the actual command content of the task that is followed,  -d  deletes the specified job, and the equivalent of the ATRM command        time format:   1  absolute time:    hh:mm   such as:13:00       At today's hh:mm time, if more than that moment, then tomorrow's time to do this task     mmdd[cc]yy, mm/dd/[cc]yy, dd.mm.[cc]yy  or [CC]YY-MM-DD      tomorrow           perform the task tomorrow   2  relative time        now+ #UNIT   Perform this task after  now + 5minutes 5 minutes              minute, hour, day, week  3  Blur Time         midnight  Midnight        noon       Noon        teatime   Afternoon

Example :

#建立at tasks [[email protected] at]# at now + 2minutesat>/bin/ls/home/roger/test >/home/roger/test/at_testat> <e ot>-----------------------------------"command last use ctrl+d appear <EOT> delegate end Job 3 at 2015-09-07 20:01# view at task [[email     Protected] at]# at-l3 2015-09-07 20:01 a root5 Tue Sep 8 15:00:00 a root6 Sat Dec 5 19:00:00 a root7 Wed Sep 9 06:17:00 a root# verification results [[email protected] test]$ cat at_test At_testcatfileerr_filefile1file2out_filerigh T_filetest_filetest_greptest_touchtest_tree

Note: when the AT command is used, it will go into the environment of the at Shell to let the user execute the task command, so be careful to use the absolute path to execute the task command.

If the command in the at Shell does not have any output information, the at default does not send an email to the performer, and if you want to send an e-mail to at anyway, use the AT command to add the-M option.


Batch command: make the task execute when the system is idle.

Batch tasks: Scheduled tasks are performed when the CPU workload is less than 0.8, and execution is deferred when the system load is too high.

The usage of batch is similar to at, andbatch has no parameters but the task executes when the system is idle

#使用batch建立空任务 [[email protected] ~]# batchat> at> at> at> <eot>job 1 at 2015-09-07 22:29

Crontab: Recurring Task Schedule

The execution of the crontab command relies on the Crond service

The same crontab exists for restricted files :

--/etc/cron.allow

will be able to write to the file using Crontab account, each user occupies a row, if not in this file is not available crontab

--/etc/cron.deny

will not be able to use the Crontab account to write to the file, if not recorded to the user, you can use the crontab

log File

Every task that Cron performs will be recorded in the/var/log/cron log file!

Recurring tasks fall into two categories:

    • system cron task ; No default running user identity, so you need to specify the additional runner;
      /etc/crontab edit this file to set a recurring task

      Vim Command Editor/etc/crontab


  • [[email protected] ~]# vim /etc/crontab shell=/bin/bash    ====== =============== "which Shell interface path=/sbin:/bin:/usr/sbin:/usr/bin    =========" is used to perform the file lookup path mailto= root    ================= "If there is extra standout, send the data to whom by email home=/    ========= ================= "Default this shell's home folder is located # for details see man 4 crontabs# example &NBSP;OF&NBSP;JOB&NBSP;DEFINITION:#&NBSP:---------------- minute  (0 - 59) # |   .------------- hour  (0 - 23) # |  |  .---------- day  of month  (1 - 31) # |  |  |  .------- month   (1&NBSP;-&NBSP;12)  or jan,feb,mar,apr ...# |  |  |  | &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

7 fields:
Top 5 fields: Point in time
User-name: Run the task as a user
command to being executed: to run a task

    • User Cron Task : committed by a user, the default is to run as the submitter, so there is no need to specify the additional runner; the/var/spool/cron/username user Cycle task is recorded in the file

VIM command; not recommended
crontab command: Recommended CRONTAB-E

  • # 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

    6 fields less performing user identities than system

    5 Time points:
    Minutes: valid value range 0-59;
    Hours: 0-23
    Day: 1-31
    Month: 1-12
    Weeks: 0-7 0 and 7 are for Sunday
    Note : the date of the month and the number of weeks, not recommended to use at the same time;
    For example:
    6 * * * Indicates the 6th point per day

Time notation:
*: Each time point in the valid value range of the time point;
-: A specific continuous time range, 3-7

1-10 * * * command every day from 1 o'clock to 10 points per hour of 10 minutes to perform a task
,: A discrete point in time, 3,5,7

0 1,3,5 * * * Command 1 points per day
/#: The amount of time in a valid time frame, for specifying the frequency;
* */4 * * Command executes a task every 4 hours

0 */4 * * Command executes a task every 4 hours


5 */3 * * */bin/echo "Hello" means the output "Hello" is executed once every 3 hours
5 7 * * 1-5/bin/echo "Hello" means 7:05 output "Hello" per day from Monday to Friday


crontab command:
crontab [-u user] [-l |-r |-e]
-u User: Not to manage your own cron task, but to specify the target user's cron task; Root has the ability to manage other users ' cron tasks; default management of their own;
-l:list, list the tasks;
-r:remove, remove all tasks, if you want to remove a task using the-e option to edit
-e:edit, edit, open a default editor for the current shell session to edit the cron task table;

When there is an execution result or there is output data in the execution option, an email will be sent to the task performer, or the account set by mailto.

Note :

1 If you do not want to receive notification messages for task execution results:
COMMAND >/dev/null Redirect, error message output
COMMAND &>/dev/null Dual orientation, both right and wrong information are not output

2 for crontab file,% has a special function, if the command will appear in the%, remember to escape, or use single quotation marks to its reference;

3 Crontab's Path variable is not exactly the same as the user's variable, so it is recommended that the task in cron use an absolute path

Example:

#列出所有任务 [[email protected] ~]$ CRONTAB-L*/2 * * * */home/roger/sh/meminfo.sh0 0 * * */sbin/shutdown-h now0 * * */BI N/mail Roger 

Anacron tool: crontab execution ls for processing Linux systems that are not 24-hour long-starting

Anacron can not specify a certain time to perform a task, but in days or immediately after the Anacron operation, detect the shutdown period should be carried out but did not perform the crontab task, and the task is executed again, and then the Anacron will automatically stop .

Anacron run time usually has two: one is the system boot period, and the other is written to the crontab schedule.


Practice:
1, every Monday to Saturday 3:20 A.M., run the CP command to archive the/etc/directory, storage location is/BACKUPS/ETC-YYYY-MM-DD;

#创建系统cron任务/etc/crontab# 1 Create/backups directory [[email protected] ~]# mkdir/backups# 2 Vim/etc/crontab Create a scheduled Task 3 * * 1-6 root /bin/cp-r/etc//backups/etc-' Date +\%f ' */2 * * * * root/root/sh_test/fstab_backup.sh

Note:crontab-l can not find the system cron task, currently I know the method is Cat/etc/crontab


2, every Sunday 2:30 A.M., run the CP command to backup the/etc/fstab file, the storage location is/BACKUPS/FSTAB-YYYY-MM-DD-HH-MM-SS

#创建用户cron任务/var/spool/cron/username #前提/backups directory already exists, if there is no need to create a #/root/sh_test/fstab_backup.sh file, edit the content:/BIN/CP/ etc/fstab/backups/fstab-' Date +\%f-\%h-%m-%s ' #查看/root/sh_test/fstab_backup.sh permissions to ensure that the performer has execute permissions on the file [[email protected] sh_test]# lltotal 4-rwxr--r--. 1 root root 8 11:11 fstab_backup.sh# set up user cron task [[email protected] ~]# crontab-e# edit/var/spool/cron/root file Content 30 2 * * 0/root/sh_test/fstab_backup.sh# View the root user cron task list [[email protected] ~]# CRONTAB-L30 2 * * 0/root/sh_test/fstab_backup. Sh

3, every night 12 o'clock, get all the lines in the/proc/meminfo file starting with S or M, appended to the/statistics/meminfo.txt file, and the daily message before, to add a similar =============== separator line;

 #创建用户cron任务   /var/spool/cron/USERNAME   #前提创建/ Statistics Directory # Create  /root/sh_test/meminfo.sh file, edit content to:/bin/echo ============================ > > /statistics/meminfo.txt | /bin/egrep  ' ^ (s| M) '  /proc/meminfo >> /statistics/meminfo.txt# view meminfo.sh permissions to ensure that performers have execute permissions [[email  Protected] sh_test]# lltotal 8-rwxr--r--.  1 root root  56 sep   8 11:11 fstab_backup.sh-rwxr--r--.  1 root root 129 Sep   8 13:24 meminfo.sh# set up user cron tasks [[email protected] ~]# crontab -e# Edit/var/ Spool/cron/root file content for 0 0 * * * /root/sh_test/meminfo.sh# view root user cron Task List [[email  Protected] ~]# crontab -l30 2 * * 0 /root/sh_test/fstab_backup.sh0  0 * * * /root/sh_test/meminfo.sh 


If there is any mistake, please correct me!

Thank you!

Linux Task Scheduler At,cron

Related Article

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.