Linux Scheduled Tasks

Source: Internet
Author: User

1. Summarize the detailed usage of task plan (at, crontab) on Linux system

Task Scheduling on Linux

Task Schedule:
Perform a task at a time in the future: at Batch
To perform a task periodically: crontab

e-Mail Service:
SMTP: Simple Mail Transfer Protocol Easy Mail Transmission protocol
/var/spool/mail/username (file that holds a user's mail)
The local mail Service (25 port) is turned on by default, sending
POP3: Post Office Protocol
IMAP4: Internet access agreement to receive mail from the receiving network

Mail command:

By default Enter the Exchange Mail transceiver (open mail box), enter the message number to view the message content

-S "Double quotation mark endogenous message subject, message body needs to be provided interactively
Mail-s "HI" [email protected]

Enter redirect to send file contents as message content
Mail-s "HI" [email protected] </path/to/somefile

Use pipelines to deliver content to your messages
COMMAND | Mail-s "HI" [email protected]


AT command:

To host a job that runs at a future time:
Support for using job queues:
Default to a queue
Job queue can only be a, B single character

Ctrl+d submitting jobs under the mail interactive interface

At "option" ... Time
Time:
(1) Absolute time:
hh:mm
Mmdd[cc]yy,mm/dd/[cc]/yy,
Refers to the time at which the next point of time is executed
Tomorrow mark tomorrow sometime.
(2) Relative time
now+ #UNIT
Minute,hour,day,week
(3) Blur time
Midnight 00:00
Noon 12:00
Teatime 16:00

Common options:
-Q queue: Specifies the name of the queues for the at job
-f/path/to/somefile execution of files from the specified path
-L View the list of jobs waiting to run in the specified job queue
At-q b-l
-C Look at the running job
-D: Delete the specified job equivalent to ATRM

The command in the commit in the at job suggests using absolute paths

Batch
Execution time is chosen by the system to run the specified task when the resource is more idle


Crontab: Recurring Task Schedule

Daemon: The Crond system is running in the background after booting, monitoring the execution time of tasks on the task table.

There are two types of recurring tasks:
(1) System cron Task: The user identity is not run by default, so additional designation of the runner is required to exist in/etc/crontab
# 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
7 fields:
Top 5 fields: Point in time
User-name: Run the task as a user
command to being executed: to run a task


(2) User Cron Task: Submitted by a user, default
exists in/var/spool/cron/username
Some commands in the file may be confidential and do not allow the user to view
It is not recommended to use the VIM command to view
Recommended use of the crontab command

# 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
# |  |  |  | |
# * * * * * command to be executed
5 Time points:
Minutes: valid value range 0-59;
Hours: 0-23
Day: 1-31
Month: 1-12
Weeks: 0-7
Note: The date of the month and the number of weeks, not recommended to use at the same time;
Time notation:
*: Each time point in the valid value range of the time point;
-: A specific continuous time range, 3-7
,: A discrete point in time, 3,5,7
/#: The amount of time in a valid time frame, for specifying the frequency;
1-30/4, */4
5 */3 * * */bin/echo "howdy"
5 7 * * 1-5/bin/echo "Howdy"


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;
Complementary tools: Anacron:
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;
(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

2, 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;

3 * * 1-6/bin/cp-rf/etc/*/backup/etc-' date + $F '

3, 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;

30 02 * * *    /bin/cp/etc/fstab   /backup/fstab-$(date+%Y-%m-%d-%H-%M-%S)

4, 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;

Might/bin/echo ' ======= ' >>/statistios/meminfo.txt &&/bin/egrep ' ^[s| M] '/proc/meminfo >>/statistios/meminfo.txt

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.