Linux Scheduled Tasks At&batch&crontab

Source: Internet
Author: User

Scheduled Tasks
Perform a task once: At,batch
To run a task periodically: crontab

1.at command, execute only once At/atq/atrm/batch
At [option] ... time
HH:MM[YYYY-MM-DD]//
Noon,midnight,teatime//12:00,24:00,16:00
Tomorrow hh:mm
now+#
Unit:minutest,hours,days,weeks
-l list tasks//equivalent to ATQ
-F reads the task from the specified file instead of the interactive input
-D job_number Delete, equivalent to ATRM
-c//view work content
At-c 8//View job content, the current user logoff, of course, will also be executed, AT-C will show all the environment
-Q indicates the queue

Error: Can ' t open/var/run/atd.pid to signal ATD. No ATD running?
Service ATD on
Chkconfig ATD on//boot up
At 1+min//CTRL +D Submit
The at job has a queue, denoted by a single letter, by default with a queue
At-f B.task Now+2min
Note: Job execution results are sent by mail to the user who submitted the job
Note: The AT environment may not be the same as the logon terminal environment, you may need to specify the full path of the command
Note: First At.allow, after At.deny, a user occupies a row, no spaces are allowed
/etc/at.allow
/etc/at.deny

2.batch//Let the system choose to perform the specified task at idle time, no need to specify the time

3.crontab
/etc/cron.allow
/etc/cron.deny
Service Program:
Cronle: Main Package
Service Crond [Start|reload|stop|restart]

Ensure that the Crond Daemon (daemon) is in a running state
Systemctl Status Crond.service
Service Crond Status
The crontab task is divided into two categories:
System-level tasks: primarily for the maintenance of the system itself//For example, automatic cleanup of temporary files
Manual editing:/etc/crontab file
User Cron Task:
/var/spool/cron/username file with the same name
Command: crontab command

Time-of-day Month Week command
Week: Sun,mon,tue,wed,thu,fri,sat/or 0-6

System level: [[email protected] test]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root

# 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 execute

1. Each row defines a recurring task with a total of 7 fields
2.user-name the user identity of the running task, the user level does not need
3. The execution result message is sent to the specified user

System level: [[email protected] test]# Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
# 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# |  |  |  | |
# * * * * * command to be execute
1. Each row defines a recurring task with a total of 6 fields
2. The environment variables here are different from the environment obtained after landing, so it is recommended to use the absolute path of the command, or to customize the path variable
3. Execute result message sent to current user
4. Time notation
1) Week and day recommendations and day only use one, not simultaneous use
2) * All values in the range of valid values at a given point in time
3) Discrete value
Multiple values separated by commas: #,#,#
9 8 * 3,7//Every Wednesday, Sunday 8:09
* 8,20 * 3,7//Every Wednesday, Sunday 8:00 and 20:00 per minute execution
4) Continuous value
#-#
5) Define the step length at the specified time
/#
*/7 * * * *//5min per run
The first few for each 7min, 8th 7 56min When the last one does not execute
Next one hours, from 0 new Start No 7min
Attention:
(1) When the specified point of time cannot be divisible by stride length, its meaning will not be the village
(2) The minimum time is min, want to complete the second task, need to rely on other mechanisms
Define per-minute tasks, and execute them multiple times within a minute in a script
5.crontabs
crontab [-u user] File
crontab [-u user] [-l |-r |-e] [-I.] [-S]
CRONTAB-N [hostname]
Crontab-c
-I ask y/y before remove
-e Edit
-R will delete/var/spool/cron/username files
Delete Single: Crontab-e The beginning of the line plus #, or delete the row
Note: The result of the run is notified to the current user by mail, or if the message is rejected
(1) COMMAND >/dev/null//Accept only the wrong mail
(2) COMMAND &>/dev/null//Neither accept mail
When you define the command, you need to escape the \% if there is a%
or ' single quote '

6.anacron for resuming execution of non-performing tasks
In days or immediately after the start of the Anacron action,
It detects crontab tasks that should be carried out during the outage, but does not
and run the task over and over, the Anacron will automatically stop
ll/etc/cron*/*ana*//To see the detection time of the Anacron
The syntax of the Anacron command is as follows
-S starts running the work continuously, according to the time record to determine whether
-F enforcement, without judging time stamps such as time logging
-N Immediate non-ongoing tasks without delaying wait time
-U simply upgrades the timestamp of the record, etc., without doing any work

The Anacron configuration file is/etc/anacrontab, and many of its contents are stored in/var/spool/anacron.
When Anacron releases Anacron-s cron.daily, it will have the following steps:
(1) The number of days from/etc/anacrontab analysis to cron.daily This job name is one day.
(2) The timestamp of the most recent run Anacron was taken out by/var/spool/anacron/cron.daily.
(3) Compare the time stamp taken with the current timestamp, and if the difference is more than one day, prepare the command.
(4) If the order is ready, the delay will be 65 minutes according to the/etc/anacrontab configuration.
(5) After the delay time, start to run the subsequent command, that is, run-parts/etc/cron.daily this sequence of commands.
(6) After the operation is complete, the Anacron program ends.


Think: A task at a specified time because the shutdown failed to execute, the next boot will not be automatically executed?
No!
If you expect an event to fail to execute on time, the next time you turn it on or not, you can use Anacron

Note: Both at and Crontab have daemons
Note:/etc/cron.day

Summary: At,batch Crontab,anacron

Practice
1. Every 12 hours to back up once/etc directory to/backups directory, save file name format "ETC-YYYY-MM-DD-HH.TAR.XZ"
0 */12 * * * L tar jcf/tmp/test/etc-$ (date +%f-%h-%s). tar.xz/etc
2. Weekly 2,4,7 backup/var/log/secure files to/logs directory, file name format "SECURE-YYYYMMDD" "
0 0 * * 2,4,7
3. Remove the line information in the current system/proc/meminfo file that starts with S or M in/tmp/meminfo.txt for every two hours
* */2 * * * grep "^[s| M] "/proc/meminfo >>/tmp/meminfo.txt//Note: The current user has write permission to Meminfo.txt, otherwise permission denied


Problem:
There is no run-parts in/etc/crontab
How to run files such as/etc/cron.daily

As shown in the Run-parts section of the file, it uses the Run-parts script to perform/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/cron.monthly
Cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly
Run-parts is a script file, which is a simple shell script that iterates through the target folder and executes the executable permission file in the first level directory.

This article is from the "Dark Horse vacated" blog, please be sure to keep this source http://hmtk520.blog.51cto.com/12595610/1950157

Linux Scheduled Tasks At&batch&crontab

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.