Linux Task Scheduler, Crontab, at, Anacron

Source: Internet
Author: User

Sometimes it is necessary to perform some system maintenance work that is time-consuming and resource-intensive, or some repetitive operations such as daily backups. The best way to do this is to schedule these tasks at a time of minimal system load at night. Crontab, at commands are often used on Linux systems to implement scheduled tasks. Scheduled tasks are divided into one-time scheduled tasks, and recurring scheduled tasks.

One-time scheduled task: The task executes only once and does not go through the second

Commands you can use: at, bath

Recurring task execution: Repeat the same task until the user deletes the task.

Commands you can use: Crontab, Anacron


AT command:

Interactive: Allow users to enter multiple commands to execute at the at > prompt;

Batch: Writes the commands of a task to a file, which is called by at


1. Interactive Planning tasks

[[email protected] ~]# at now+3min \ \ defines a task to be performed after 3 minutes at> ls/var \ \ command to execute, if there are more than one can use enter to break the line continue input at> <EOT> \ \ Ctrl+djob 3 at 2016-03-18 09:08


2. Batch processing create a scheduled task

Use-F to specify the script file to execute

[[email protected] ~]# at Now+1min-f/root/backup.sh job 4 at 2016-03-18 09:17


3. View Job Queue

[[email protected] ~]# at-l1 2016-03-18 09:30 a root2 2016-03-18 09:40 a root[[email protected] ~]# atq1 2016-03 -18 09:30 a root2 2016-03-18 09:40 a root


4. Delete Job

[[email protected] ~]# at-d 2 \ 2 represents the number of the job [[email protected] ~]# atq1 2016-03-18 09:30 a root

The execution result of the task plan will be sent to the task submitter by mail;

Blur Time:
Now, noon noon, Midnight Midnight, Teatime 4 o'clock in the afternoon, tomorrow tomorrow



**********************************

Crontab Set up timed tasks

crontab [-L |-r |-e |-u user]

Common options:
-L: View your cron task list;
-E: Open the user's own cron profile through the editor defined in the editor variable;
Editing individual tasks uses the-e option, whether deleting, modifying, or creating a new one;
-r: Remove the crontab file

-U UserName: Configure crontab jobs for other users, only administrators can perform this operation.

(a) The Crontab mission plan needs to rely on Crond services, to be effective when the mission plan comes into operation

# service Crond Statuscrond (PID 1814) is running ...


(b) Several commonly used documents related to Crontab

System Cron File

/etc/crontab
User cron File:
/var/spool/cron/username
Task Schedule Blacklist list

Users who do not allow users to schedule tasks can write to this file

/etc/at.deny


(c), cron execution results for those who do not have a user, the successful execution will be sent to the root user, for some do not need to receive the message can be redirected through the output to reject the message:
&>/dev/null
>/dev/null 2>&1
Mailto= ""



/etc/crontab files: Each line defines a separate task;

shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# 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


The format of each line in the file is shown below

Minute hour date Month Week command

Minutes: Any number of integers from 0~59.

Hours: Any integer from 0-23.

Date: Any integer from 1-31. (If the month you specify, it must be a valid date for that month)

Month: Any integer from the 1~12.

Week: Any integer from 0~7 (both 0 and 7 represents Sunday), or you can use Sun,mon,tue,wed,thu,fri,sat.

In the above values, you can use the * sign to denote all values, such as the * number of minutes, indicating any number between 0~59.


Time notation:
1. Each time bit should use the value within the range of valid values available to it;
2. * On a certain time position indicates all valid values of the Keio position;
3,-: Consecutive time of the adjacent points to take the value;
4,,: Discrete point of time to take the value;
5./#: Indicates every # once in the specified time range;
5-45/3 * * * *


Note 1: If you use% in crontab user commands, you have to escape to \%

*/1 * * * */bin/tar-jcf/tmp/etc. ' Date +\%f-\%s '. tar.xz/etc/*

NOTE 2: The command in the crontab task is best to use the full path such as/bin/tar



Practice:
1, every 3 minutes to perform an "echo" How is it? " ;

*/3 * * * */bin/echo "How is it?"


2, weekly 2, 4, 6 backup/etc/directory to the/backup directory, the file name of the backup to be the beginning of the etc_ and keep up with the date of the day as the file name;

0 0 * * 2,4,6/bin/tar-jcf etc_ ' date +%f '. tar.xz/etc/*


3, daily 6, 9, 12, 15, 16, 17, 18 View all the file systems mounted on the current system, and append the results of the view to the/tmp/mounts.txt file;

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


4, every two hours per day to take the current system memory space allowance, save it to the/stats/memory.txt file;

0 */2 * */bin/grep "memfree:"/proc/memoryinfo >>/stats/memory.txt

Or

0 */2 * * * [-d/stats] | | /bin/mkdir/stats,/bin/grep "Memfree:"/proc/meminfo >>/stats/meminfo.txt

Crontab file Format:
Blank rows are ignored
# The line at the beginning is a comment;


How to implement a second-level task:
* * * * * for i in {0..4}; Do/bin/echo "How is it?"; Sleep 10; Done
Perform a task every 21 seconds

Like this is not an integer time, can only be used in a dead loop way to do

While true:; Do/bin/echo "How is You?" Sleep 21; Done

Anacron: The minimum scale is the day;
is a supplement to crontab to check whether a task in the crontab has not been executed in the past period,
If it is not executed, it will be executed once at some point after power-on, regardless of whether its cycle arrives or not;
2 3 * * * some_job

This article is from "Rookie Diary" blog, please make sure to keep this source http://zkxfoo.blog.51cto.com/1605971/1752496

Linux Task Scheduler, Crontab, at, Anacron

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.