The crontab command cron job

Brief introduction

crontab commands to be executed periodically provided
Linux system Linux mobilization divided into two categories:
  1, the work performed by the system: The system of work to be performed periodically, such as backing up the system data, clear the cache
  2, the work performed by a person: a user work to be done on a regular basis, such as every 10 minutes to check the mail server for new letter, which work by each user set their own
/ Etc / crtontab
  System scheduling tasks generally stored in / etc / crontab file, stored inside a system running the scheduler via the command cat crontab View
  These tasks will be up and running in the system automatically calls
/ Var / spool / cron
  Each user generates its own crontab automatically generates a file, typically located at / var / spool / cron directory
  Run crontab -r will remove the current user's crontab file. For example, you switch to the next suifeng account, execute the command, / var / spool / cron / suifeng file will be deleted, if you want to create the file just use the crontab -e command.
  Note that ordinary users generally do not have permission to access / var / spool / cron
/etc/crontab.deny file with the file /etc/crontab.allow
  1, respectively, the user can not use the crontab commands, and the user can use the command crontab
  2, two files exist crontab.allow file priority
  3, neither of these files exist, only the root user has invoked the command
 

 

 

grammar

crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]

Each Parameter Description

  1. -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)
  2. -r [UserName]: Delete the current time-table
  3. -l [UserName]: list the current time-table
  4. -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.
(.Sh script can be written to a command file, and can be given executable permissions)

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

crontab -l

To delete a scheduled task

crontab -r
 

Examples of Use

Example 1: a command execution every 1 minute
  * * * * * command
Example 2: 3 and every hour in the first 15 minutes of
3,15 * * * * command
Example 3: 3 and performed in the first 15 minutes of 8:00 to 11:00
3,15 8-11 * * * command
Example 4: 3 and performs the first 15 minutes of every day 8:00 to 11:00
3,15 8-11 */2 * * command
Example 5: 3 and the implementation of the first 15 minutes of every Monday 8:00 to 11:00
3,15 8-11 * * 1 command
Example 6: night 21:30 smb restart 
30 21 * * * /etc/init.d/smb restart
Example 7: 1 month, 10, 22, the 4:45 restart smb 
45 4 1,10,22 * * /etc/init.d/smb restart
Example 8: every Saturday and Sunday 1:10 to restart the smb
10 1 * * 6,0 /etc/init.d/smb restart
Example 9:18 per day: smb restart every 30 minutes 00: 00-23 
0,30 18-23 * * * /etc/init.d/smb restart
Example 10: Every Saturday night, 11: 00 pm restart smb 
0 23 * * 6 /etc/init.d/smb restart
Example 11: smb restart every hour 
* */1 * * * /etc/init.d/smb restart
Example 12: between 11 pm to 7 am, every hour restart smb 
* 23-7/1 * * * /etc/init.d/smb restart
Example 13: Monthly No. 4 and 11:00 from Monday to Wednesday to restart the smb 
0 11 4 * mon-wed /etc/init.d/smb restart
Example 14: 1 January of 4:00 smb restart 
0 4 1 jan * /etc/init.d/smb restart
Example 15: execute scripts in a directory /etc/cron.hourly per hour
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
Example 16:
  1 day, 23:59 test.sh background execution script; and written to standard output, log.txt
  59 23 * * * /home/suifeng/scripts/test.sh >/home/suifeng/log.txt &
  2, ibid.
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
  3, daily 23:59 test.sh the background script; and error output to write a book, log.txt
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
  4, daily 23:59 test.sh the background script; and wrong as wrong standard output (standard output and error output is about all written in log.txt)
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt 2>&1 &

Instructions

  0 indicates that the keyboard input
  1 for standard output
  2 shows the error output
  & Means the background
  >> represents additional
  > Write to cover representation

note

  1, when a job is running in the foreground, the terminal is occupied by the job; and a job in the background, it does not occupy the terminal. You can use the command & work in the background.
  2, do not require user interaction command in the background, because it looks like the machine will have to wait on that
  3, the job is running in the background, as the results will be output on the screen, interfere with your normal work. So we would redirect the output to a file
 

crond service

By chkconfig --list | grep cron cron command to view the situation to start service:
[Root @ ***** home] # chkconfig --list | grep cron crond 0: Close 1: Close 2: Enabled 3: Enable 4: Enable 5: 6 Enable: Off
We can see that the system start level if it is 1-4, cron service will boot automatically activated, we can start to set up the service entry with the following command:
  /sbin/service crond start
  /sbin/service crond stop
  /sbin/service crond restart
  /sbin/service crond reload
More rows 1-4 respectively to start, stop, restart services, and reload the configuration.
Cron should start automatically set at boot time, add / sbin / service crond start to the /etc/rc.d/rc.local script.
View crontab service status:
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命令,很快就能掌握的,网上也有相关资料和教程,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。

 

个人经验总结

  我们使用这个命令大多都是运行相关的脚本文件,最常见的就是定时调用http请求进行数据采集或者数据库的备份等等。因此当我们自己所写的脚本文件中含有文件引入时,最好采用绝对路径,否则将会引入失败从而导致定时任务失败。
 
 
 

简介

crontab用于设置周期性被执行的命令
Linux系统中Linux调动工作分为两大类:
  1、系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存
  2、个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置
/etc/crtontab
  系统调度的任务一般存放在/etc/crontab这个文件下,里面存放了一些系统运行的调度程序,通过命令cat crontab查看
  这些任务会在系统运行起来自动调用
/var/spool/cron
  每个用户都会生成自动生成一个自己的crontab文件,一般位于/var/spool/cron目录下
  执行命令crontab -r 就会删除当前用户的crontab文件。例如你切换到suifeng账号下,执行了该命令,那么/var/spool/cron/suifeng文件就会删除,如果要创建该文件只需要用crontab -e命令即可。
  注意,普通用户一般没有权限访问/var/spool/cron
/etc/crontab.deny文件与/etc/crontab.allow文件
  1、分别表示不能使用crontab命令的用户、以及能够使用crontab命令的用户
  2、两个文件同时存在crontab.allow文件优先
  3、两个文件都不存在则只有root用户才拥有调用该命令
 

 

 

语法

crontab [-e [UserName]|-l [UserName]|-r [UserName]|-v [UserName]|File ]

各个参数说明

  1. -e [UserName]: 执行文字编辑器来设定时程表,内定的文字编辑器是 vi(可以指定别的用户,但是必须拥有权限)
  2. -r [UserName]: 删除目前的时程表
  3. -l [UserName]: 列出目前的时程表
  4. -v [UserName]:列出用户cron作业的状态

创建定时任务

  crontab -e进入文本(vi)编辑状态
  * * * * * command path  // 前五个字段可以取整数值,指定何时开始工作,第六个域是字符串,即命令字段,其中包括了crontab调度执行的命令。 各个字段之间用spaces和tabs分割。
(可将命令写入.sh脚本文件中,并赋予可执行的权限即可)

调度命令的规则

字段名称 说明 取值范围
分钟 每小时的第几分钟执行 0-59
小时 每日的第几个小时执行 0-23
日期 每月的第几天执行 1-31
月历 每年的第几月执行 1-12
星期 每周的第几天执行 0-6
命令名称 欲执行的命令及参数  

crontab命令中的一些常用特殊符号

符号 说明
* 表示任何时刻
, 表示分割
表示一个段,如第二段里: 1-5,就表示1到5点
/n 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.

查看定时任务

crontab -l

删除定时任务

crontab -r
 

使用示例

实例1:每1分钟执行一次command
  * * * * * command
实例2:每小时的第3和第15分钟执行
3,15 * * * * command
实例3:在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * command
实例4:每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * command
实例5:每个星期一的上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1 command
实例6:每晚的21:30重启smb 
30 21 * * * /etc/init.d/smb restart
实例7:每月1、10、22日的4 : 45重启smb 
45 4 1,10,22 * * /etc/init.d/smb restart
实例8:每周六、周日的1 : 10重启smb
10 1 * * 6,0 /etc/init.d/smb restart
实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb 
0,30 18-23 * * * /etc/init.d/smb restart
实例10:每星期六的晚上11 : 00 pm重启smb 
0 23 * * 6 /etc/init.d/smb restart
实例11:每一小时重启smb 
* */1 * * * /etc/init.d/smb restart
实例12:晚上11点到早上7点之间,每隔一小时重启smb 
* 23-7/1 * * * /etc/init.d/smb restart
实例13:每月的4号与每周一到周三的11点重启smb 
0 11 4 * mon-wed /etc/init.d/smb restart
实例14:一月一号的4点重启smb 
0 4 1 jan * /etc/init.d/smb restart
实例15:每小时执行/etc/cron.hourly目录内的脚本
01 * * * * root run-parts /etc/cron.hourly
# run-parts这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是目录名
实例16:
  1、每天23点59分后台执行test.sh脚本;并将标准输出写入log.txt中
  59 23 * * * /home/suifeng/scripts/test.sh >/home/suifeng/log.txt &
  2、同上
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
  3、每天23点59分后台执行test.sh脚本;并将错误输出写书log.txt中
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt &
  4、每天23点59分后台执行test.sh脚本;并将错误输错当做标准输出(即将错误输出和标准输出全部写入log.txt中)
  59 23 * * * /home/suifeng/scripts/test.sh 1>/home/suifeng/log.txt 2>&1 &

相关说明

  0表示键盘输入
  1表示标准输出
  2表示错误输出
  &表示后台执行
  >>表示追加
  >表示覆盖写

注意

  1、当在前台运行某个作业时,终端被该作业占据;而在后台运行作业时,它不会占据终端。可以使用&命令把作业放到后台执行。
  2、需要用户交互的命令不要放在后台执行,因为那样子机器将会在那一直等待
  3、作业在后台运行时,一样会将结果输出在屏幕上,干扰你的正常工作。因此我们最好将输出重定向到某个文件中
 

crond服务

通过 chkconfig --list | grep cron 命令来查看cron服务的启动情况:
[root@***** home]# chkconfig --list | grep cron crond 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
我们可以看到,系统启动级别如果是1-4,cron服务都会开机自动启动的,我们可以通过如下命令来设置该服务的启动项:
  /sbin/service crond start
  /sbin/service crond stop
  /sbin/service crond restart
  /sbin/service crond reload
以上1-4行分别为启动、停止、重启服务和重新加载配置。
要把cron设为在开机的时候自动启动,在 /etc/rc.d/rc.local 脚本中加入 /sbin/service crond start 即可。
查看crontab服务状态:
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命令,很快就能掌握的,网上也有相关资料和教程,介绍的非常详细,感兴趣的话,可以搜一下,希望以上分享的内容能对你有所帮助吧,也欢迎大家评论、留言进行补充。

 

个人经验总结

  我们使用这个命令大多都是运行相关的脚本文件,最常见的就是定时调用http请求进行数据采集或者数据库的备份等等。因此当我们自己所写的脚本文件中含有文件引入时,最好采用绝对路径,否则将会引入失败从而导致定时任务失败。
 
 
 

Guess you like

Origin www.cnblogs.com/diandianquanquan/p/11518977.html