First, summarize the task plan on Linux system at, crontab detailed use method
1. Planning Task Description
Perform a task at a time in the next point: At,batch;
Perform a task periodically: crontab;
2. e-Mail Service
Scheduled tasks send results to users who perform tasks by default in the form of a message
Mail command:
Mail username (@hostname): Enter the interactive send and receive mail interface
Mail-s ' SUBJECT ' [email protected]: Set message subject
</path/from/somefile: Send file content to recipients
echo "XXX" | Mail-s ' SUBJECT ' [email protected]: send echo output to Recipient
3. At command use
At [option] ..... Time
(1) Common options
- q queue:at Job Queue
-f/path/from/somefile: Reads the job to run from the specified file
-L: View the list of jobs that are running in the job queue (equals the ATQ command)
-C At_job_num: Look at the contents of the Run job
-D Number: Deletes the specified job, equivalent at RM
(2) Time format
Absolute time:
hh:mm,
Mmdd[cc]yy, Mm/dd/[cc]yy, dd.mm.[cc]yy or [cc]yy-mm-dd
Relative time:
now+ #UNIT (now+3minute)
Minute, hour, day, week
Blur Time:
midnight,noon,teatime
[Email protected] ~]#/etc/init.d/atd statusatd is stopped[[email protected] ~]#/etc/init.d/atd startstarting ATD: [OK] [Email protected] ~]# at 17:19at> ls/etcat> cat/etc/fstabat> <eot>job 2 at 2015-09-08 17:19you with Mail In/var/spool/mail/root
4. Batch: The system chooses to run the specified task when the resource is idle.
5. Crontab: Recurring Task plan
Daemon: Crond
[Email protected] ~]#/etc/init.d/crond statuscrond (PID 1977) is running ...
(1) System cron Task: No user identity is run by default, so you need to specify additional runner
Configuration file:/etc/crontab
(2) User Cron Task: Submitted by a user, run as submitter by default (CRONTAB-E recommended)
Configuration file:/var/spool/cron/username
(3) Configuration file field parsing
[[email protected] ~]# cat /etc/crontabshell=/bin/ bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# for details see man 4 CRONTABS#&NBSP;EXAMPLE&NBSP;OF&NBSP;JOB&NBSP;DEFINITION:#&NBSP---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | . ------- month (1 - 12) OR jan,feb,mar,apr ...# | | &NBSP;|&NBSP;&NBSP;|&NBSP;&NBSP---- day of week (0 - 6) (sunday=0 or &NBSP;7) or sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be Executed
*****:5 Time Point: (A few hours and weeks)
Minutes: 0-59
Hours: 0-23
Day: 1-31
Weeks: 0-7
Months and weeks not recommended for simultaneous use
User-name: Run the task as a user
command to be executed: the task to run (command)
Instance:
Annual: 2 3 4 5 *
Monthly: 20 18 6 * *
Daily: 20 9 * * *
Weekly: 17 16 * * *
Per hour: 3 * * * *
Per minute: * * * * * *
Point-in-time notation:
1. *: Each time point within the valid value range of the time point
2.-: Indicates a specific continuous time range (3 * * 1-5 command)
3,,: denotes discrete point of time (2 * * 2,4,6 command)
4./#: one time unit per # in the effective time range; Used to specify the frequency ()
*/3 * * * * command: executed every 3 minutes
* */3 * * Command: Every three hours on arrival, every minute of this hour is executed
6 */3 * * Command: No three hours to execute
Note: It is not accurate to 7 minutes at a time (sleep mechanism can be used)
(4) crontab command
crontab [-u user] [-l] [-r] [-e]
- u User: Not managing your own cron task, but specifying the target user's cron task; Root has the right to manage other users ' cron tasks default to manage your own
-l:list, List tasks
-r:remove, remove All Tasks
-e:edi T, edit, open a default editor for the current shell session to edit the Cron task table
Note:
(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
/root/bin/a.sh (including commands to be used in the script)
Second, 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;
[[email protected] ~]# mkdir-pv/backupsmkdir:created directory '/backups ' [[email protected] ~]# Crontab-eno crontab fo R root-using an empty ONE20 3 * * 1-6/bin/cp-a/etc/backups/etc-' Date +f% '
three, 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;
2 * * 7/bin/cp-a/etc/fstab/backup/fstab-' date +f '
12 o'clock every night, get all lines in the/proc/meminfo file that begin with S or M, append to the/statistics/meminfo.txt file, and precede the daily message with a similar =============== separator line
[[email protected] ~]# mkdir-pv/statisticsmkdir:created directory '/statistics ' 0 0 * * */bin/echo-e "===========\n" &&/bin/grep "^[s| M] "/proc/meminfo >>/statistics/meminfo.txt
How to use the Task Scheduler command at and Crontab on Linux systems