Linux has two scheduled execution services, Crond and ATD. At tasks that run only once, or how much time it takes to boot.
I. Brief introduction of Crond (centos6.5)Crond is a daemon that is used to periodically perform certain tasks or wait for certain events under Linux, similar to Scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default and the Crond process is started automatically.the Crond process periodically checks for tasks to be performed every minute, the task is automatically executed if there is a task to perform. The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling. System task scheduling: The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. /etc/crontab file, this is the System Task Scheduler configuration file. The/etc/crontab file includes the following lines:
[Email protected] ~]# cat/etc/crontabshell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto= "" home=/# RUN-PARTS51 * * * * Root run-parts/etc/cron.hourly24 7 * * * Root run-parts/etc/cron.daily22 4 * * 0 root Run-parts/etc /CRON.WEEKLY42 4 1 * * Root run-parts/etc/cron.monthly
The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script. Restart command can only be written in the system/etc/crontab to take effect, personal crontab is not effective. User Task scheduling: Users to perform regular work, such as user data backup, scheduled email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. Its file name is the same as the user name. CRONTAB-E creates the root user's crontab file by default, so it is equivalent to editing/var/spool/cron/root the specified user to create a timed task, create a regular user's scheduled task, based on permissions considerations, Some tasks that do not require root can be created with ordinary users.
Crontab-u User name-e #编辑指定用户的crontab任务crontab-u user name-L #查看指定用户的crontab任务
Second, Crond service
/sbin/service crond Start// start service/sbin/service crond stop //shutdown service/sbin/service crond restart//Restart service/sbin/ Service Crond Reload //Reload Configuration
Special Symbolic meaning
* |
Represents every meaning, such as the first * represents every minute, the second * represents an hour. |
, |
Represents a discontinuous time, such as 00 9,21 * * * representing 9 points per day and 21:00 each. |
- |
The continuous time range, such as 00, 30 9-15 * * 1-5, represents the daily between 9 o'clock and 15 points from Monday to Friday, respectively, in 00 and 30 minutes. |
*/n |
Every once in a while, */5 * * * *, perform a task every 5 minutes. |
定时任务脚本基本语法
#print the hello* * * * */bin/sh /script/my.sh >/dev/null 2>&1
最好写上
/bin/sh
,这样即使脚本忘记加X权限,也可以顺利执行。
bash脚本要写全路径,不然
crontab
会搜索用户home目录下的同名文件。
每个任务都应该添加注释,以方便自己或者别人查阅维护。
Third, examples
* * * * command executes command every minute (because cron scans every 1 minutes by default, so it's all *) 00,30 * * * * command every hour of No. 00 and 30th Minutes to execute command, can also be written as *
/30* * * * * 3,15 8-11 */2 * * Command every 2 days in the morning 8-11 o ' clock 3rd and 15 minutes to execute command 15,45 8-11 * * 1 command each Monday 8 o'clock in the morning to 11 o ' clock 15th and 45 minutes Execute command * * * */ETC/INIT.D/SMB restart 21:30 restart SMB 4 1,10,22 * */ETC/INIT.D/SMB restart per 1, 10, 22nd, 4:45 restart SMB 1 * * 6,0/ETC/INIT.D/SMB restart every Saturday, Sunday of 1:10 restart SMB 0,30 18-23 * */etc/init.d/netwo RK Restart 00 minutes and 30 minutes between 18 and 23 daily Restart Network 0 1 * * */bin/sh/script/aaa.sh daily 1 o'clock Full execution aaa.sh 0 4 * 1-5/etc/init . D/SMB Restart such writing is not standardized, the general day and week do not use simultaneously, there will be uncertainty in time.* */1 * * * */ETC/INIT.D/SMB restartRestart SMB every hour? This is wrong because the first minute is *, so this task executes every minute, not hourly.
Four, backup and recoveryCrontab-l > $HOME/mycron #备份crontab内容到home下的mycroncrontab mycron #导入备份文件 [[email protected] ~]# Cronta b mycron[[email protected] ~]#[[email protected] ~]# crontab-l .... #start nginx after boot05 * * */bin/sh/script/star t_nginx.sh >/dev/null 2>&1 #* */1 * * * echo $ (date) ' 123 ' >/dev/pts/0 */10 * * * * echo $ (date +\%f-\%h-\%m) >/data/log Precautions:
1:crond 服务
默认每分钟检测是否有需要执行的任务,有的话就执行。
2:对于秒级的任务crond无法实现。
秒级任务可以通过shell脚本,然后加入
sleep
实现。
#!/bin/bash
while
true
echo
‘
hello word!‘ >>
/tmp/a
.log
sleep
1
#间隔执行时间为1秒
do
done
3:定时任务写在bash脚本里执行,不要直接在crontab里写命令。
crontab
里%会有问题,所以
date
+%F带%号会有错误,写在bash里就没问题。
可以在%号前加\%让系统识别。
4:定时任务后加 >
/dev/null
2>&1,取消结果输出到屏幕。
如果确实需要保存日志,可以后面加 >
/a
.log 2>&1 将输出追加到指定文件。
5:最好写上
/bin/bash
,这样即使脚本忘记加X权限,也可以顺利执行。
sh脚本要写全路径,不然
crontab
会搜索用户home目录下的同名文件。
每个任务都应该添加注释,以方便自己或者别人查阅维护等。
6:crontab
-e 默认是root用户的定时任务
/var/spool/cron/root
有些定时任务不需要root那么高的权限的话,可以指定相关账户执行。
crontab
-u hsm -e
编辑hsm用户的
crontab
定时任务。
crontab
-u hsm -l
查看hsm用户的
crontab
定时任务。
7:定时任务也可以写在
/etc/crontab
里,不过这是系统的定时任务文件,一般不要写在这里面。
8:环境变量不能直接在
crontab
里调用,一定要写在脚本里,使用前最好提前声明。
在脚本里export 声明。
Linux basic services, Crond of Scheduled Tasks