1. Summarize the detailed usage method of the task plan (at, crontab) on Linux system;
There are 3 ways to plan your Linux tasks
1.batch: By the system to choose when the resources are more idle run the specified task, do not need to specify a point in time, this is not used, not much introduced
2.at: Used to perform a task at a specified time
Usage: at [option] ... Time
A) Enter the at> command prompt environment,
b) Enter the task that needs to be performed, and then enter Ctrl+d to indicate the submission task
c) The final result is sent to the user by mail, can be viewed using the Mail command, can be SS-TNL to see whether the mail port 25 is turned on
d) Time,at need to develop execution time
(1) Absolute time: the means of expression oh, yes.
HH:MM, the next time to arrive at this point in time
Mmdd[cc]yy, Mm/dd/[cc]yy, dd.mm.[cc]yy or [cc]yy-mm-dd
Tomorrow
(2) Relative time: Now+[minute, hour, day, week], which means that it has been engraved for too long
(3) Blur time: Midnight,tomorrow,noon,teatime
Example:
[Email protected] ~]# at Now+1minute
at>/bin/echo "Hello World"
At> <EOT>
Job 4 at Mon Sep 14 22:35:00 2015
[[Email protected] ~]# mail
Message 6:
From [email protected] Mon Sep 14 22:31:00 2015
Return-path: <[email protected]>
X-original-to:root
delivered-to: [Email protected]
Subject:output from your Job 3
To: [Email protected]
Date:mon, Sep 22:36:00 +0800 (CST)
From: [Email protected] (root)
Status:ro
Hello World
Common options:
-Q queue:at Job queue, identifier ABCDEFG, default to a, can be easily managed by grouping queues
-f/path/from/somefile: Reads the job to be run from the specified file;
If directly through the at> input can not have typos, because can not be changed, so it is best to use-F, the job to be executed in the document, through-F calls the files in the job.
Because it is fetched from an environment variable, it is inconsistent with the path you perform the at operation, so it is recommended that the execution in the job use absolute paths to avoid errors
[Email protected] ~]# at-f echo.sh-q b now+1minute
Job 5 at Mon Sep 14 23:21:00 2015
[Email protected] ~]# ATQ
5 Mon Sep 23:21:00 B Root
1 Tue Sep 10:19:00 a root
-L: View a list of such running jobs in the job queue; equivalent to using the ATQ command;
[Email protected] ~]# ATQ
1 Tue Sep 10:19:00 a root
[Email protected] ~]# at-l
1 Tue Sep 10:19:00 a root
-C At_job_num: Look at the contents of the running job;
[Email protected] ~]# At-c 6
There's a bunch of things in the middle that indicate the environment variables, it doesn't look good, I deleted it.
${shell:-/bin/sh} << ' Marcindelimiter608226e0 '
#!/bin/bash
/bin/echo "Hello word"
Marcindelimiter608226e0
-D: Deletes the specified job, equivalent to ATRM, deletes the job and notifies you of the message.
[Email protected] ~]# at-d 1
[Email protected] ~]# at-l
You have a new message in the/var/spool/mail/root
3.crontab: Used to run a recurring task schedule
Crontab needs to use a daemon (crond), always running in the background, and performing tasks through the service when the next task time arrives.
Crontab's mission is divided into 2 categories.
System Cron Task: The task is not running the user identity by default, so you need to specify the additional runner. configuration file is/etc/crontab; not recommended!
User Cron Task: Submitted by a user, run as submitter by default, no additional runtime required, configuration file is/var/spool/crom/username
The task is written in a file, but is not visible to ordinary users, it is recommended to use the crontab command to define the task
The configuration file format is as follows, edited by Crontab-e:
# 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
Crontab contains a total of 7 fields:
Top 5 fields: Point in time, everything with * means each time, 34 of which will not be used in conjunction with 5, and it contains the relationship
*: Each time point in the valid value range of the time point;
-: A specific continuous time range, 3-7 means 3 minutes to 7 minutes
,: A discrete point in time, 3,5,7 represents 3 minutes, 5 minutes, 7th minutes
/#: The number of times in the effective time range for the specified frequency; */3 means every 3 hours 1-30/4 means 1-30 minutes every 4 minutes
User-name: User cron does not need this option to run the task as a user.
command to being executed: to run a task
5 */3 * * */bin/echo "Hello World" run the next command every 3 hours for the 5th minute
* */4 * * * */bin/echo "Hello World" means every 4 hours after the command is run every minute
5 7 * * 1,5/bin/echo "Hello World" means to run a command 7:5 every day from Monday to Friday
When the task runs out of mail to the user, to confirm that the user has permission to run these commands, be sure to use an absolute path or unexpected circumstances will occur
If you don't know the absolute command of this command, you can use the which command to find
Can be viewed through crontab-l.
crontab command usage: 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;
CRONTAB-E-U PDU
-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: Booting after a specified point in time will run all the tasks that were previously running
Note the following points:
(1) If you do not want to receive notification messages for task execution results:
COMMAND >/dev/null not send mail successfully
COMMAND &>/dev/null not succeed or email
(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;
In addition, if you define a cron task, and eventually you want to be able to receive a reminder email to prove that your task is being executed properly or not being executed properly, then we can change the Mail command in Linux to send mail to your working mailbox as follows:
Modify the/etc/mail.rc, and add the following to the file
Set from= your e-mail address
Set smtp= your mail server address, note must be public IP
Set smtp-auth-user= your email account or username
Set smtp-auth-password= your e-mail password
Set Smtp-auth=login
Then use the join mailto command in crontab
MAILTO = Your email address, if not followed by the email address will not send the message
[[email protected] ~]# crontab-l-u PDU
[Email protected]
1 * * * */bin/echo "Hello word"
3 * * 1-6/bin/cp-a/etc/*/backup/etc-' Date +\%f '
2 * * 7/bin/cp-a/etc/fstab/backup/fasb-' date +\%f-\%h-\%m-%s '
0 0 * * * root/bin/echo "===============================" >>/statistics/meminfo.txt &&/bin/cat/proc/mem info | Egrep "^s|^m" >>/statistics/meminfo.txt >/dev/null 2>&1
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;
CORNTAB-E-U PDU
[[email protected] ~]# crontab-l-u PDU
3 * * 1-6/bin/cp-a/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;
[[email protected] ~]# crontab-l-u PDU
2 * * 7/bin/cp-a/etc/fstab/backup/fasb-' date +\%f-\%h-\%m-%s '
4, every night 12 o'clock, get all the lines in the/proc/meminfo file starting with S or M, append to/statistics/meminfo.
TXT file, and the daily message is preceded by a similar =============== separator line;
[[email protected] ~]# crontab-l-u PDU
0 0 * * * root/bin/echo "===============================" >>/statistics/meminfo.txt &&/bin/cat/proc/mem info | Egrep "^s|^m" >>/statistics/meminfo.txt >/dev/null 2>&1
Linux Task Plan Summary