Linux Basics---Scheduled tasks

Source: Internet
Author: User
Tags set time

    • Scheduled Tasks

The so-called scheduled task is to perform one or more specific jobs (tasks) at a specific time in the future. Implementation of the job without human intervention.


I. Classification of SCHEDULED Tasks

Depending on the number of times the scheduled task is executed in the future, it is divided into one-time scheduled tasks and recurring scheduled tasks.

A one-time scheduled task: Executes only once in the future.

Recurring Scheduled Tasks: Execute multiple times in a certain time in the future.


二、一次 Sexual planning tasks

The commands commonly used for one-time scheduled tasks are at,batch.

1. Set up one-time scheduled tasks

1) at command sets a one-time scheduled task, which is managed by the ATD background process. To set up a one-time task you need to start the ATD service process. Start the service in CentOS with the/ETC/INIT.D/ATD start command.

Under the command line:

At time

At> COMMAND1

At> COMMAND2

...

At>

CTRL+D: Submitting a task

The formats supported by Tmie are:

format example
hh:mm 13:12
mmddyy mm/dd/yy  dd. Mm. YY  yyyy-mm-dd 030914 03/09/14 09.03.14 2014-03-09
am PM 4am 3pm
specific character + 

now noon  Midnight teatime

+{minutes|hours|days|weeks}

Combination using

Now + 3min

4pm + 1days

Teatime + 3hours 

td>


[[email protected] ~]# at now + 10minat> ls/etc/passwdat> cat/etc/issueat> date +%f-%dat> <eot>job 4 A T 2014-07-12 11:33[[email protected] ~]# at 2pmat> ls/etc/passwdat> <eot>job 5 at 2014-07-12 14:00[[email Pro Tected] ~]# atq42014-07-12 11:33 a root52014-07-12 a root[[email protected] ~]# at 17:00at> dateat> <EOT> ; job 6 at 2014-07-12 17:00

At is managed through the job queue, viewing the job queue by the at latter at-l command

[[email protected] ~]# atq62014-07-12 17:00 a root #前面的数字是作业的ID号, delete the job can be used when the 82014-07-12 11:31 a root52014-07-12 a ro Ot

Delete a task at-d job ID or ATRM job ID that you do not want to perform

[[email protected] ~]# atq62014-07-12 17:00 a root52014-07-12 a root[[email protected] ~]# ATRM 6[[email protected] ~]# atq52014-07-12 a root[[email protected] ~]#

After the job executes, it notifies the submitter of the job in the form of local mail.

You can control whether users can use at to set a one-time task by using/etc/at.allow and/etc/at.deny 2 files.

2) The batch function is the same as at, but there is no need to specify a specific time compared to at. system resources are selected to perform the specified job when the comparison is idle.

[Email protected] ~]# batch at> ls .at> <eot>job at 2014-07-12 11:42you has mail in/var/spool/mail/root# It executes immediately, because now is the time when the system is idle.

Ii. Periodic tasks

A recurring task is to cycle through a particular job at a specific time in the future. is through the cron mechanism to complete the corresponding function. is being monitored by the background process Crond.

Use the "/etc/init.d/crond status" command statement to view the status of the Crond, if the status is stopped., you need to start the service, execute the/etc/init.d/crond start;chkconfig crond On ".

/etc/init.d/crond Status View the state of Crond

/etc/init.d/crond Stop shutdown Crond Service

/etc/init.d/crond Start Open Crond Service

1. Cron Task classification

Depending on the performer of the cron task, Cron is divided into two categories:

    • System cron

Defined in/etc/crontab, each row defines a separate task, which is targeted at the system level.

    • User cron

defined in the/var/spool/cron directory, each user has a file with the same name as the user name, which functions like/etc/crontab, each defining a separate task. This is for user-level.

2. Related catalogue and documents

/etc/cron.hourly/the directory of scripts to be executed per hour

/ect/cron.daily/the directory of scripts to execute every day

/etc/cron.monthly/the directory of scripts to execute every month

/etc/cron.weekly/the directory of scripts to be executed per week

/etc/cron.deny only users listed in the Cron.allow file can use the Cron service while ignoring the Cron.deny file

/etc/cron.allow If the Cron.allow file does not exist, the user listed in the Cron.deny file will be banned from using the cron service

3, the setting of periodic tasks

Environment variables and formatting for recurring tasks:

    • Environment variables:

shell=/bin/bash# Default Shell Interpreter

Path=/sbin:/bin:/usr/sbin:/usr/bin #默认环境PATH的路径, so sometimes it's time to write a full script path when setting a recurring task.

Mailto=root #如果出现错误, or have data output, data sent to this account as mail

home=/#默认的家目录


    • Format:

#&nbsp:---------------- minute  (0 - 59) # |  .------------- hour  (0 - 23) # |  |  .---------- day of month  (1 -  31) # |  |  |  .------- month  (1 - 12)  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# mainly consists of 3 parts, the first part  * * * The time represented by  * *  is: minute hour day month day# * * * * *     per minute  # 2 */2 * * *  every 2 hours   */#: On the valid value of the corresponding time bit every # once #  3 4 * * 1,3,5  weekly 1,3,5     ', ' stands for discrete value # 3 9-17 * * *  9-17 '-' per day Continuous interval on a certain time bit # Note: In the set time is, than it is generally determined by small units # The second part is the:user-name  user name, which is required to specify the user name when editing/etc/crontab to set a recurring task. The other two methods do not need # The third part is the  command to be executed  executable command or script. If there is a run-parts keyword followed by the directory where the script is located #1 * * * * root run-parts /etc/cron.hourly/


There are three ways to set up recurring tasks:

1) via Crontab command

crontab [-u user_name]-E

An editor is opened and can be set in the appropriate format.

2) by editing the/var/soop/user_name file settings

3) by editing the/etc/crontab file settings

4. Discard Email Notifications:

Use output redirection:

&>/dev/null

>/dev/null

5, crontab command other common usage

-L: Lists all tasks that have been defined

-E: Open the Edit Interface definition task

-R: Remove All Tasks if you want to delete a specific task, you can delete the row that contains the task by setting the three ways the cycle task is located.

6. Other

Supplementary mechanism of Anacron:crontab

Check for a task that has not been performed in a valid period in the past, and if so, execute it at a specified point in time after boot;

To implement a second-level task:

* * * * * * for i in {1..5};d o script; sleep 10;done (every 10 seconds)

7. Practice

1, once a month to perform a backup of the/directory, back up to the/backup directory, save the directory name format is rootfs-2014-07;

# 0 4 1 * * [-d/backup] | | Mkdir/backup; /bin/cp-a//backup/etc-$ (date + '%y-%m ')

2, weekly 3,5,7 backup/proc/cpuinfo file to/backup/cpu.info/directory, save the file name is cpu-2013081009;

# 3 1 * * 3,5,7 [-d/backup/cpu.info/] | | Mkdir-p/backup/cpu.info/; /bin/cp-a/proc/cpuinfo/backup/cpu.info/cpu-$ (date + '%y%m%d%h ')

3, every two hours per day to take the current system/proc/meminfo in the beginning of the information to save to/stats/memory.txt

# 2 */2 * * * grep-i "^s"/proc/meminfo >>/stats/memory.txt

4, working days of work hours, every hour to perform the ' echo ' Hello '

# 1 9-17 * * 1-5 echo "Hello"


This article from the "Night after" blog, reproduced please contact the author!

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.