Linux Recurring Task Scheduler

Source: Internet
Author: User

I. Recurring Task schedule Type:


1, one-time task Execution (At,batch)-not important:


A, at:

Interactive: Let the user enter multiple commands to execute at the at> prompt.


For example: #at 10:02, go to the AT command prompt, as follows:

#at > Ls/usr/local

#at > CAT/ETC/PASSWD


#提交任务: Ctrl+d

#查看任务计划列表: At-l

#删除一个尚未执行作业的方法: at-d job_num or ATRM job_num


Batch: Writes each command of a task to a file, called by at, format: at-f/path/to/at_job_file time

such as: #at-F at.txt 15:22, where the contents of At.txt are as follows:

Ls/usr/local

cat/etc/passwd

The execution result of a task plan: sent to the task submitter by means of a message. Messages can be viewed through the mail command.


B, Batch: Unlike at, you cannot specify a time, it automatically selects the system to execute when idle.




2. Periodic task Execution (crontab, Anacron)--Important:


A, System cron: usually defined in/etc/crontab. For example, periodically delete files under the TMP directory.


#cat/etc/crontab, each line defines a separate task, as follows:

Shell=/bin/bash

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root

home=/


# for details see Mans 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


B, user cron: usually defined in/var/spool/cron/username.

To implement user cron using the crontab command:

-L: View your cron Task List

-E: Opens the user's own cron configuration through the editor defined in the editor variable.

For example: #crontab-E, the definition reads as follows:

*/5 * * * */bin/echo "Hello cron"

-r: Remove user's crontab

If you are an administrator:

-U UserName: Configures crontab tasks for other users.

such as: #crontab-e-U Hadoop configures its tasks for Hadoop users.


Note 1: The command to execute is defined in cron, and the absolute path of the command must be used if it does not apply to a path similar to/etc/crontab.

*/5 * * * */bin/echo "Hello cron" &>/dev/null

NOTE 2: If crontab edits the file, if there is a%, transfer \% or use single quotation marks, otherwise it cannot be executed.


0 */3 * * */bin/touch ~/testfile_ ' date +\%y-\%m-\%d '. txt


0 */3 * * */bin/touch ~/testfile_ ' date + ' \%y-\%m-\%d '. txt


Note 3: If you do not want to receive messages, you can either redirect by redirection or mailto= "" If you do not wish to suggest a redirect.

*/5 * * * */bin/echo "Hello cron" &>/dev/null


Example: 6 9 12 per day, check all the file systems that are currently closed on the system, and append the results to the/tmp/mounts.txt file.

0 6,9,12 * * */bin/mount >>/tmp/mounts.txt


Note 4: Smaller than each of the units must give specific values, this value has its own to set.

Example: Take the current system memory headroom every two hours per day and save it to the/stats/memory.txt file.

0 */2 * * */bin/grep "^memfree"/proc/meminfo >>/stats/memory.txt


Note 5: Tasks that implement a second level (because the minimum time for a task is minutes)

Example: echo "Hello cron" is executed every 10 seconds

* * * * * * for i in {0..4};d o/bin/echo "Hello cron"; Sleep 10; Done


C, Time notation:

A, each time bit should use a value within its available valid range.

B, the * On a time bit indicates the valid value of the corresponding bit.

C,-: continuous point in time, such as 30-35 7 * * * 7 points per day 30-35 minutes execution, executed every minute.

D,,: Discrete point-in-time values such as: 5,15,25,35 * * * * 5 15 25 35 minutes per hour.

E,/#: Executes once every # within the specified time frame. such as: 0/20 * * * Every 20 minutes, 3-55/3 * * * is performed every three minutes between 3-55 minutes.

D, Anacron: is a cron supplement, can effectively check cron in a task in the past a certain period of time has not been performed tasks,

Execute it as soon as possible after the boot time. The point in time for this execution is defined in/etc/anacrontab.

#cat/etc/anacrontab, the contents are as follows:

Shell=/bin/sh

Path=/sbin:/bin:/usr/sbin:/usr/bin

Mailto=root

# The maximal random delay added to the base delay of the jobs

Random_delay=45

# The jobs would be started during the following hours only

Start_hours_range=3-22


#period in days delay in minutes job-identifier command

15cron.dailyNice run-parts/etc/cron.daily, tasks that have not been performed within a certain period, 5 minutes after power-on

7cron.weeklyNice run-parts/etc/cron.weekly, a task that has not been performed in a certain period and is executed 25 minutes after booting

@monthlycron.monthlyNice run-parts/etc/cron.monthly, a task that has not been performed in a certain period and is executed 45 minutes after boot



II. Mail command: Lists the mailing list. Quit the message with quit.


1, through-s to specify the subject subject, and the contents of the file/path/to/somefile as the message content, sent to username.

#mail-S "Subject" UserName </path/to/somefile


2, through-s to specify the theme subject, sent to the user, and in the > input message content, through ctrl+d submit mail.

#mail-S "Subject" UserName


Linux Recurring Task Scheduler

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.