Brief introduction
/ Etc / crtontab
/ Var / spool / cron
/etc/crontab.deny file with the file /etc/crontab.allow
grammar
crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]
Each Parameter Description
- -e [UserName]: the implementation of a text editor to set the time-table, the default text editor is vi (you can specify other users, but must have permission)
- -r [UserName]: Delete the current time-table
- -l [UserName]: list the current time-table
- -v [UserName]: lists the status of the user cron jobs
Create a scheduled task
crontab -e enter text ( VI ) state edit * * * * * path Command // first five fields can take an integer value to specify when to start the work, the sixth field is a string, i.e. the command field, including crontab scheduling command execution. Splitting spaces between the tabs and the fields.
Rules scheduling command
Field Name | Explanation | Ranges |
minute | The first few minutes of every hour | 0-59 |
hour | The first few hours of daily execution | 0-23 |
date | The first few days of the month to perform | 1-31 |
Monthly calendar | The first few months every year of execution | 1-12 |
week | Weekly day of execution | 0-6 |
Command Name | Commands and parameters to be executed |
crontab command of some commonly used special symbols
symbol | Explanation |
* | Represent any moment |
, | That indicates the segment |
- | Denotes a segment, such as the second paragraph: 1-5, it is 1 to 5:00 |
/n | N represents the execution time of each unit, such as the second paragraph, * / 1, it means that every one hour to perform a command. It can also be written as 1-23 / 1. |
View regular tasks
To delete a scheduled task
Examples of Use
* * * * * command
3,15 * * * * command
3,15 8-11 * * * command
3,15 8-11 */2 * * command
3,15 8-11 * * 1 command
30 21 * * * /etc/init.d/smb restart
45 4 1,10,22 * * /etc/init.d/smb restart
10 1 * * 6,0 /etc/init.d/smb restart
0,30 18-23 * * * /etc/init.d/smb restart
0 23 * * 6 /etc/init.d/smb restart
* */1 * * * /etc/init.d/smb restart
* 23-7/1 * * * /etc/init.d/smb restart
0 11 4 * mon-wed /etc/init.d/smb restart
0 4 1 jan * /etc/init.d/smb restart
01 * * * * root RUN-Parts / etc / cron.hourly # RUN -parts this argument, if this parameter is removed, then later you can write a script name to run, instead of the directory name
59 23 * * * /home/suifeng/scripts/test.sh >/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt 2>&1 &
Instructions
note
crond service
/sbin/service crond start /sbin/service crond stop /sbin/service crond restart /sbin/service crond reload
service crond status
Operation Example
1. First, the timing of the implementation of the new need to shell script, and here I explain the problem in order to facilitate, create a script that prints the current time date.sh, reads as follows, very simply, is to date the command line, and then redirect the output to a date .txt text:
2. Next is to add the script to the date.sh timed task, directly run "crontab -e" command to add on the line, as follows, where the first to execute every minute script, for example, to see whether the normal scheduled task execution, editing " * / 1 * * * * /root/date.sh "save on the line, as follows:
As for the meaning of these six fields, official documents are described in detail below, representing the commands minutes, hours, days, months, weeks and execution, online presentation materials is also very rich, interested, you can search:
3.最后,我们再查看一下输出文件date.txt和crontab日志,如下,说明添加的定时任务已经正常运行,这里再替换成你每天定时执行的时间,只需要修改第一、二个字段就行,例如,每天10:30执行,则编辑“30 10 * * * /root/date.sh”保存就行:
至此,我们就完成了每天定时执行shell脚本。总的来说,整个过程非常简单,只要你有一定的Linux基础,熟悉一下crontab命令,很快就能掌握的,网上也有相关资料和教程,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。
个人经验总结
简介
/etc/crtontab
/var/spool/cron
/etc/crontab.deny文件与/etc/crontab.allow文件
语法
crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]
各个参数说明
- -e [UserName]: 执行文字编辑器来设定时程表,内定的文字编辑器是 vi(可以指定别的用户,但是必须拥有权限)
- -r [UserName]: 删除目前的时程表
- -l [UserName]: 列出目前的时程表
- -v [UserName]:列出用户cron作业的状态
创建定时任务
crontab -e进入文本(vi)编辑状态 * * * * * command path // 前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spaces和tabs分割。
调度命令的规则
字段名称 | 说明 | 取值范围 |
分钟 | 每小时的第几分钟执行 | 0-59 |
小时 | 每日的第几个小时执行 | 0-23 |
日期 | 每月的第几天执行 | 1-31 |
月历 | 每年的第几月执行 | 1-12 |
星期 | 每周的第几天执行 | 0-6 |
命令名称 | 欲执行的命令及参数 |
crontab命令中的一些常用特殊符号
符号 | 说明 |
* | 表示任何时刻 |
, | 表示分割 |
- | 表示一个段,如第二段里: 1-5,就表示1到5点 |
/n | 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1. |
查看定时任务
删除定时任务
使用示例
* * * * * command
3,15 * * * * command
3,15 8-11 * * * command
3,15 8-11 */2 * * command
3,15 8-11 * * 1 command
30 21 * * * /etc/init.d/smb restart
45 4 1,10,22 * * /etc/init.d/smb restart
10 1 * * 6,0 /etc/init.d/smb restart
0,30 18-23 * * * /etc/init.d/smb restart
0 23 * * 6 /etc/init.d/smb restart
* */1 * * * /etc/init.d/smb restart
* 23-7/1 * * * /etc/init.d/smb restart
0 11 4 * mon-wed /etc/init.d/smb restart
0 4 1 jan * /etc/init.d/smb restart
01 * * * * root run-parts /etc/cron.hourly # run-parts这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是目录名
59 23 * * * /home/suifeng/scripts/test.sh >/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt 2>&1 &
相关说明
注意
crond服务
/sbin/service crond start /sbin/service crond stop /sbin/service crond restart /sbin/service crond reload
service crond status
操作实例
1.首先,新建需要定时执行的shell脚本,这里我为了方便说明问题,新建了一个打印当前时间的脚本date.sh,内容如下,非常简单,就是一行date命令,然后将输出重定向到一个date.txt文本中:
2.接着就是将这个date.sh脚本添加到定时任务中,直接运行“crontab -e”命令添加就行,如下,这里先以每分钟执行一次脚本为例,看定时任务能否正常执行,编辑“*/1 * * * * /root/date.sh”保存就行,如下:
至于这6个字段的含义,官方文档有详细说明,如下,分别代表分、时、天、月、星期和执行的命令,网上介绍资料也非常丰富,感兴趣的话,可以搜索一下:
3.最后,我们再查看一下输出文件date.txt和crontab日志,如下,说明添加的定时任务已经正常运行,这里再替换成你每天定时执行的时间,只需要修改第一、二个字段就行,例如,每天10:30执行,则编辑“30 10 * * * /root/date.sh”保存就行:
至此,我们就完成了每天定时执行shell脚本。总的来说,整个过程非常简单,只要你有一定的Linux基础,熟悉一下crontab命令,很快就能掌握的,网上也有相关资料和教程,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。