Linux Basics---Scheduled tasks
Scheduled Tasks
The so-called scheduled task is to perform one or more specific jobs (tasks) at a specific time in the future. Implementation of the job without human intervention.
I. Classification of SCHEDULED Tasks
Depending on the number of times the scheduled task is executed in the future, it is divided into one-time scheduled tasks and recurring scheduled tasks.
A one-time scheduled task: Executes only once in the future.
Recurring Scheduled Tasks: Execute multiple times in a certain time in the future.
二、一次 Sexual planning tasks
The commands commonly used for one-time scheduled tasks are at,batch.
1. Set up one-time scheduled tasks
1) at command sets a one-time scheduled task, which is managed by the ATD background process. To set up a one-time task you need to start the ATD service process. Start the service in CentOS with the/ETC/INIT.D/ATD start command.
Under the command line:
At time
At> COMMAND1
At> COMMAND2
...
At>
CTRL+D: Submitting a task
The formats supported by Tmie are:
Format Example
hh:mm 13:12
MMDDYY mm/dd/yy DD. Mm. YY yyyy-mm-dd030914 03/09/14 09.03.14 2014-03-09
AM PM 4am 3pm
Specific characters + now noon Midnight Teatime
+{minutes|hours|days|weeks}
Combination using now + 3min
4pm + 1days
Teatime + 3hours
[Email protected] ~]# at now + 10min
At> ls/etc/passwd
At> cat/etc/issue
At> Date +%f-%d
At> <EOT>
Job 4 at 2014-07-12 11:33 <ctrl+d Save Task Schedule >
[Email protected] ~]# at 2pm
At> ls/etc/passwd
At> <EOT>
Job 5 at 2014-07-12 14:00
[Email protected] ~]# ATQ
4 2014-07-12 11:33 a root
5 2014-07-12 a root
[[email protected] ~]# at 17:00
At> Date
At> <EOT>
Job 6 at 2014-07-12 17:00
At is managed through the job queue, viewing the job queue by the at latter at-l command
[Email protected] ~]# ATQ
6 2014-07-12 17:00 A root #前面的数字是作业的ID号, can be used when deleting a job
8 2014-07-12 11:31 a root
5 2014-07-12 a root
Delete a task at-d job ID or ATRM job ID that you do not want to perform
[Email protected] ~]# ATQ
6 2014-07-12 17:00 a root
5 2014-07-12 a root
[Email protected] ~]# ATRM 6
[Email protected] ~]# ATQ
5 2014-07-12 a root
[Email protected] ~]#
After the job executes, it notifies the submitter of the job in the form of local mail.
You can control whether users can use at to set a one-time task by using/etc/at.allow and/etc/at.deny 2 files.
2) The batch function is the same as at, but there is no need to specify a specific time compared to at. system resources are selected to perform the specified job when the comparison is idle.
[[Email protected] ~]# batch
At> ls.
At> <EOT>
Job at 2014-07-12 11:42
You have mail in/var/spool/mail/root
#它立即就执行了, because now is the time when the system is idle.
Ii. Periodic tasks
A recurring task is to cycle through a particular job at a specific time in the future. is through the cron mechanism to complete the corresponding function. is being monitored by the background process Crond.
Use the "/etc/init.d/crond status" command statement to view the status of the Crond, if the status is stopped., you need to start the service, execute the/etc/init.d/crond start;chkconfig crond On ".
/etc/init.d/crond Status View the state of Crond
/etc/init.d/crond Stop shutdown Crond Service
/etc/init.d/crond Start Open Crond Service
1. Cron Task classification
Depending on the performer of the cron task, Cron is divided into two categories:
System cron
Defined in/etc/crontab, each row defines a separate task, which is targeted at the system level.
User cron
defined in the/var/spool/cron directory, each user has a file with the same name as the user name, which functions like/etc/crontab, each defining a separate task. This is for user-level.
2. Related catalogue and documents
/etc/cron.hourly/the directory of scripts to be executed per hour
/ect/cron.daily/the directory of scripts to execute every day
/etc/cron.monthly/the directory of scripts to execute every month
/etc/cron.weekly/the directory of scripts to be executed per week
/etc/cron.deny only users listed in the Cron.allow file can use the Cron service while ignoring the Cron.deny file
/etc/cron.allow If the Cron.allow file does not exist, the user listed in the Cron.deny file will be banned from using the cron service
3, the setting of periodic tasks
Environment variables and formatting for recurring tasks:
Environment variables:
Shell=/bin/bash #默认shell解释器
Path=/sbin:/bin:/usr/sbin:/usr/bin #默认环境PATH的路径, so sometimes it's time to write a full script path when setting a recurring task.
Mailto=root #如果出现错误, or have data output, data sent to this account as mail
home=/#默认的家目录
Format:
#.----------------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
#主要有3部分组成, the first part * * * * * is represented by: Minute hour day month
# * * * * * * per minute
# 2 */2 * * * Every 2 hours */#: On the valid value of the corresponding time bit per # once
# 3 4 * * 1,3,5 weekly 1,3,5 ', ' representing discrete values
# 3 9-17 * * * daily 9-17 '-' continuous interval on a certain time position
#注意: In the set time is, than it is usually a small unit is determined
#第二部分是: User-name user name, this section needs to specify a user name when editing/etc/crontab to set recurring tasks. The other two ways do not need
#第三部分是 command to be executed executable commands or scripts. If there is a run-parts keyword followed by the directory where the script is located
#1 * * * * Root run-parts/etc/cron.hourly/
There are three ways to set up recurring tasks:
1) via Crontab command
crontab [-u user_name]-E
An editor is opened and can be set in the appropriate format.
2) by editing the/var/soop/user_name file settings
3) by editing the/etc/crontab file settings
4. Discard Email Notifications:
Use output redirection:
&>/dev/null
>/dev/null
5, crontab command other common usage
-L: Lists all tasks that have been defined
-E: Open the Edit Interface definition task
-R: Remove All Tasks if you want to delete a specific task, you can delete the row that contains the task by setting the three ways the cycle task is located.
6. Other
Supplementary mechanism of Anacron:crontab
Check for a task that has not been performed in a valid period in the past, and if so, execute it at a specified point in time after boot;
To implement a second-level task:
* * * * * * for i in {1..5};d o script; sleep 10;done (every 10 seconds)
7. Practice
1, once a month to perform a backup of the/directory, back up to the/backup directory, save the directory name format is rootfs-2014-07;
# 0 4 1 * * [-d/backup] | | Mkdir/backup; /bin/cp-a//backup/etc-$ (date + '%y-%m ')
2, weekly 3,5,7 backup/proc/cpuinfo file to/backup/cpu.info/directory, save the file name is cpu-2013081009;
# 3 1 * * 3,5,7 [-d/backup/cpu.info/] | | Mkdir-p/backup/cpu.info/; /bin/cp-a/proc/cpuinfo/backup/cpu.info/cpu-$ (date + '%y%m%d%h ')
3, every two hours per day to take the current system/proc/meminfo in the beginning of the information to save to/stats/memory.txt
# 2 */2 * * * grep-i "^s"/proc/meminfo >>/stats/memory.txt
4, working days of work hours, every hour to perform the ' echo ' Hello '
# 1 9-17 * * 1-5 echo "Hello"
This article from the "eyes engraved with your Smile" blog, declined reproduced!
Linux Basics---Scheduled tasks