Linux Task Scheduler

Source: Internet
Author: User
Tags set time

Linux Task Scheduler


Note: The results of each execution of a Linux task plan, including errors, will be sent to the administrator in message format, so you must install the Mail service to view the results of the task execution


1. E-Mail Service:

Smtp:simple Mail Transmission protocol

/var/spool/mail/username

Pop3:post Office Protocol

Imap4:internet Mail Access Protocol


Mail command:

Mail: Enter the interactive email interface;

Mail-s ' SUBJECT ' [email protected]

Mail-s ' SUBJECT ' [email protected] </path/from/somefile

COMMAND | Mail-s ' SUBJECT ' [email protected]


To see if the Mail Service started: Netstat-tlnup | grep 25


2, at command: Automatically perform some pre-set command actions at the specified date and point of time, belonging to a one-off scheduled task

To host a job that runs in the future time:

Support for using job queues:

The default is a queue;

Save Task: Ctrl+d


At [option] ... Time

Time:

(1) Absolute time

HH:MM,

Mmdd[cc]yy, Mm/dd/[cc]yy, dd.mm.[cc]yy or [cc]yy-mm-dd

Tomorrow

(2) Relative time

now+ #UNIT

Minute, hour, day, week

(3) Blur time

Midnight

Noon

Teatime


Common options:

-Q queue:at Job queue;

-f/path/from/somefile: Reads the job to be run from the specified file;

-L: View a list of such running jobs in the job queue; equivalent to using the ATQ command;

C At_job_num: Look at the contents of the running job;

-D: Delete the specified job; equivalent to ATRM


Service Script Name:/ETC/INIT.D/ATD


syntax format: at {hours: minutes} {year-month-day}

ATQ: User queries the currently set at Task List

ATRM: Delete the at task with the specified task number


Example: Application of RELATED commands

[[email protected] ~]# date2015  09 month  05 day   Saturday  18:39:54 CST[[email  Protected] ~]# at 18:41 2015-09-05at> touch /aaat> <eot>job  2 at 2015-09-05 18:41[[email protected] ~]# atq2        2015-09-05 18:41 a root[[email protected] ~]# stat /aa   File:  "/AA"   Size: 0                Blocks: 0           io block: 4096    Plain Empty file device: fd00h/64768d    inode: 14           Links: 1Access:  (0644/-rw-r--r--)    Uid:  (    0/    root)    Gid:  (     0/ &Nbsp;  root) access: 2015-09-05 18:41:01.325106256 +0800modify: 2015-09-05  18:41:01.325106256 +0800change: 2015-09-05 18:41:01.325106256 +0800[[email protected]  ~]# at 23:59at> touch /bbat> <EOT>job 3 at  2015-09-05 23:59[[email protected] ~]# atq3        2015-09-05 23:59 a root[[email protected] ~]# atrm 3[[email protected]  ~]# atq

Batch

The system chooses to run the specified task when the resource is more idle;


3, Crontab: According to the pre-set time period (minutes, hours, days ...) Repeatedly executes user-specified command actions, which are periodic scheduled tasks

Service Script Name:/etc/init.d/crond

The global configuration file is located at:/etc/crontab


There are two types of recurring tasks:

(1) System cron task; There is no default running user identity, so you need to specify the additional runner;

/etc/crontab

# * * * * * * user-name command to be executed

(2) User Cron Task: Submitted by a user, the default is to run as the submitter, so there is no need to specify the additional runner;

/var/spool/cron/username

# * * * * * command to be executed


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 to remove all tasks;

-e:edit, edit, open a default editor for the current shell session to edit the cron task table;


Each field Description:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/72/AB/wKiom1Xqx5uwG-roAADWzjgMDZU927.jpg "title=" 123. JPG "alt=" wkiom1xqx5uwg-roaadwzjgmdzu927.jpg "/>

Special representations of time values:

*: Indicates any time within the range

,: Multiple discontinuous points of time representing the interval

-: Represents a continuous range of time

/: Specify the time frequency of the interval


Example: 017**1-5 Monday to Friday daily 17:003088*1,3,5 every Monday, three, five 8:30 08-18/2***8 points to 18 points each two hours 0**/3** every three days


system default settings and scripts, located in directory /etc/cron.*/

User-defined This is, located in the file /var/spool/cron/user name

Manage Cron Scheduled Tasks:

Edit Scheduled Task:crontab–e {-u user name}

View Scheduled Tasks:crontab–l {-u user name}

Delete Scheduled Task:crontab–r {-u user name}


Attention:

(1) If you do not want to receive notification messages for task execution results:

COMMAND >/dev/null

COMMAND &>/dev/null


(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;

such as:/bin/cp-rfp/etc//backups/etc-' Date +\%y\%m\%d '


(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

such as:/root/bin/a.sh


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;

Answer: 3 * * 1-6/bin/cp-rfp/etc//backups/etc-' Date +\%y\%m\%d '


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

Answer: 2 * * 7/bin/cp-rfp/etc/fstab/backups/fstab-' date +\%y\%m\%d\%h\%m\%s '


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;

Answer: 0 0 * * */bin/egrep--color=auto ' ^s|^m '/proc/meminfo >>/statistics/meminfo.txt && Echo =============== ============ >>/statistics/meminfo.txt


This article comes from "doing what you want to do!" "Blog, be sure to keep this provenance http://807257775.blog.51cto.com/2448194/1691610

Linux 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.