Linux Scheduled Tasks

Source: Internet
Author: User

There are two main tasks of Linux planning

One to perform the task once and the other to perform the task periodically


Perform tasks once

Commands: At and Batch

The AT command is an interactive input

[[email protected] ~]# at Now+1min at> ls/at> <eot>job @ 2015-09-17 22:31

First line at time, enter the command to execute and submit the task with Ctrl+d

Time: can use now+5hour, or directly input time 12:43, you can use the man at to query the timing input format


ATQ to view an existing task queue

[Email protected] ~]# ATQ132015-09-17 22:40 a root[[email protected] ~]# mailheirloom Mail version 12.4 7/29/08. Type? For help. " /var/spool/mail/root ": 1 message 1 new>n 1 root Thu Sep 22:41 35/583" Output from your Job "# The results are sent to the mailbox after the task is completed and can be viewed by mail

-D #: Delete the specified task

[[email protected] ~]# atq162015-09-17 22:52 a root172015-09-17 22:58 a root[[email protected] ~]# at-d 16[[email Protect Ed] ~]# atq172015-09-17 22:58 a root


The batch command and the AT command use are roughly the same, but cannot specify the run time and the system will automatically pick the system when it is not busy.


Recurring tasks

In CENTOS6, the crontab command is used primarily to perform periodic tasks. It is run based on a process crond, so check this process before configuring the scheduled cycle task to run

[[Email protected] ~]# service Crond statuscrond (PID 2238) is running ... [Email protected] ~]# chkconfig--list crondcrond 0:off1:off2:on3:on4:on5:on6:off

Configuring a Cron Schedule task is actually fixing two files.

1,/etc/crontab

This file modifies the system scheduling task, which can specify different user's scheduled tasks, only root can modify

Note that it also provides the environment variable when the cron task runs, because the user is not logged on when running the task, so the system environment variables cannot be used. If you need to run a program file, it is a good idea to specify the full pathname, or modify the file.

The root user receives the execution result file, not the task's execution user Mai

[[email protected]ntos ~]# 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) # |  | &NBSP;&NBSP:---------- day of month  (1 - 31) # |  |  | &NBSP;&NBSP:------- month  (1 - 12)  OR jan,feb,mar,apr ...# |  &NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP---- day of week  (0 - 6)   ( SUNDAY=0&NBSP;OR&NBSP;7)  OR sun,mon,tue,wed,thu,fri,sat# |  |  |   |  |# *  *  *  *  * user-name command  to be executed 

The annotations, which are the representation of the defined cycle time, are represented by a left-to-right asterisk, minutes, hours, days of the month, month, day of the week

Now for example, if you let User2 run program LS/in every 5 minutes, add a command to the/etc/crontab file

*/5 * * * * user2 LS/

1, * Equivalent to all time-point wildcard characters, if all are used *, it means that every minute of every hour of the month is run

If you want to run it once a day 0 0 * * * user2 echo "Test" This means to run 12:0 midnight every day

If you change to * 0 * * * * user2 echo "test" means that there are no minutes to run every day at 12 o'clock Midnight, which is equivalent to running 60 times a day.

2, crontab can also be set to run every few minutes, or several hours

*/5 * * * * * run every 5 minutes

0 */5 * * * run every 5 hours

3, you can also specify an execution interval

0-6/2 * * * * * every 2 minutes in 0-6 minutes per hour

4. Specify execution time directly

0 2,4,6 * * * Day of the Morning 2, 4, 6 point execution


2. The second configuration scheduled task file is the/var/spool/cron/user name

This file can be edited between, and the difference above is not to specify the running user name

You can also edit with commands

CRONTAB-E open a temporary edit document to edit the current user's scheduled tasks

Crontab-e-user User2 Edit a scheduled task for a specified user

Crontab-r or Crontab-r-user user2 undo all Scheduled Tasks or scheduled tasks for a specified user, in fact, delete the scheduled task file

Crontab-l or Crontab-l user user2 View all or the specified scheduled tasks, but cannot view scheduled tasks for/etc/crontab


Other tasks are configured in the same way that the configuration command file has special symbols that require an escape character \ Example \%


Linux Scheduled Tasks

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.