当前位置: 代码迷 >> 综合 >> Linux 定时任务 cron - daemon to execute scheduled commands
  详细解决方案

Linux 定时任务 cron - daemon to execute scheduled commands

热度:5   发布时间:2024-01-05 09:39:06.0

题记:最近在做自动下载build的脚本,脚本已经测试完成了,要让脚本在固定时间run,需要使用linux的定时任务,一下是从网上摘录和整理的有关linux的资料

 

可以参考IBM的developerworks上的文章http://www.ibm.com/developerworks/cn/linux/l-job-scheduling.html

一命令使用

        cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:

       /sbin/service crond start      //启动服务

  /sbin/service crond stop         //关闭服务

  /sbin/service crond restart     //重启服务

  /sbin/service crond reload      //重新载入配置

  你也可以将这个服务在系统启动的时候自动启动:

  在/etc/rc.d/rc.local这个脚本的末尾加上:

  /sbin/service crond start

  现在Cron这个服务已经在进程里面了,我们就可以用这个服务了

 

 

 

二脚本编写

相关命令----------------

     crontab file [-u user]-用指定的文件替代目前的crontab。
     crontab-[-u user]-用标准输入替代目前的crontab.
     crontab-1[user]-列出用户目前的crontab.
     crontab-e[user]-编辑用户目前的crontab.
     crontab-d[user]-删除用户目前的crontab.
     crontab-c dir- 指定crontab的目录。
     crontab文件的格式:M H D m d cmd.
M: 分钟(0-59)。
H:小时(0-23)。
D:天(1-31)。
m: 月(1-12)。
d: 一星期内的天(0~6,0 表示星期天)
   除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。

 

 

先前曾提到,crontab 的格式分成六个部分,前五个是时间参数。在上例中你会发现除了数字与英文名称,有使用到符号"*",这个符号代表每一单位的意思,譬如 30 3 * * * 既代表 30分 3点 每日 每月 星期的每天。

时间的指定,可以是单一的数字,或几个数字用逗号来连接。看下例

30 3,12 * * * /root/fbin/bak-web
其中的第二项为 3,12,这代表 3 以及 12 小时的意思。再来看下例

30 */6 * * * /root/fbin/bak-web
我把第二项改成 */6 这代表每 6 小时,也相当於 6,12,18,24 的作用。此外还有一个区段的做法

30 8-18/2 * * * /root/fbin/bak-web
我把第二项改成 8-18/2 这代表在 8 小时到 18 小时之间每 2 小时,也相当於 8,10,12,14,16,18 的作用。

 

-------------------------------------

 三demo

 

以Linux下定时备份mysql为例说明下

写一个简单的mysql备份shell脚本

vi

#!/bin/sh
da=`date +%Y%m%d%H%M%S`
mysqldump -u root -pdongjj --all-database>/root/mysqlbakup/$da

保存为 mysqlbak.sh

然后在终端输入crontab-e会出现一本文本编辑状态

 0 3 * * * /root/mysqlbak.sh 

保存退出

 即,每天3点整开始执行mysql数据库的备份脚本

 

 

Example Cron File

# use /bin/sh to run commands, no matter what /etc/passwd says

SHELL=/bin/sh
# mail any output to 'paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day(每天午夜零点5分,运行$HOME/bin/daily.job 。。。。命令)
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul(每个月的1号下午2点15分运行$HOME/bin。。命令)
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe(周1到周5,也就是工作日的晚上10点整运行mail命令)
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%


四crond和atd以及利用已有的crontab脚本

crond and atd are two very simple and important services that everyone should be familiar with. crond does the job of running commands periodically (daily, weekly), while atd's main feature is run a command once at some future time.
 These two services are so basic that we are not going to detail their package contents and invocation.
 
/etc/crontab configuration file
 The /etc/crontab file dictates a list of periodic jobs to be run -- like updating the locate and whatis databases, rotating logs, and possibly performing backup tasks. If there is anything that needs to be done periodically, you can schedule that job in this file. /etc/crontab is read by crond on startup. crond will already be running on all but the most broken of UNIX systems.
 After modifying /etc/crontab, you should restart crond with /etc/rc.d/init.d/crond restart.
 /etc/crontab consists of single line definitions for what time of the day/week/month a particular command should be run.
 Each line has the form:

 
 <time> <user> <executable>

 

<time> is a time pattern that the current time must match for the command to be executed. <user> tells under what user the command is to be executed. <executable> is the command to be run.
 The time pattern gives the minute, hour, month-day, month, and week-day that the current time is compared. The comparison is done at the start of every single minute. If crond gets a match, it will execute the command. A simple time pattern is as follows.

 

 
 50 13 2 9 6 root /usr/bin/play /etc/theetone.wav

 

Which will play the given WAV file on Sat Sep 2 13:50:00 every year, while

 
 50 13 2 * * root /usr/bin/play /etc/theetone.wav

 

will play it at 13:50:00 on the 2nd of every month, and

 
 50 13 * * 6 root /usr/bin/play /etc/theetone.wav

 

will do the same on every Saturday. Further,

 
 50 13,14 * * 5,6,7 root /usr/bin/play /etc/theetone.wav

 

will play at 13:50:00 and at 14:50:00 on both Friday, Saturday and Sunday, while

 
 */10 * * * 6 root /usr/bin/play /etc/theetone.wav

 

Will play every 10 minutes the whole of Saturday. The / is a special notation meaning ``in steps of''.
 In the above examples, the play command is executed as root.
 The following is an actual /etc/crontab file:

 

 

 
 # Environment variables first
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

 

# Time specs
30 20 * * * root /etc/cron-alarm.sh
35 19 * * * root /etc/cron-alarm.sh
58 18 * * * root /etc/cron-alarm.sh
01 * * * * root run-parts /etc/cron.hourly(在/etc/cron.hourly目录下面放入你需要每小时执行的脚本,不用修改什么就可以自动每小时执行该脚本了,其他的类似)
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

 

 

 

 Note that the # character is used for comments as usual. crond also allows you to specify environment variables under which commands are to be run.
 Your time additions should come like mine have, to remind me of the last three Metro trains of the day.
 The last four entries are vender supplied. The run-parts command is a simple script to run all the commands listed under /etc/cron.hourly, /etc/cron.daily etc. Hence, if you have a script which needs to be run every day, but not at a specific time, you needn't edit your crontab file: rather just place the script with the others in /etc/cron.<interval>.
 My own /etc/cron.daily/ directory contains:

 

 

 total 14
drwxr-xr-x    2 root     root         1024 Sep  2 13:22 .
drwxr-xr-x   59 root     root         6144 Aug 31 13:11 ..
-rwxr-xr-x    1 root     root          140 Aug 13 16:16 backup
-rwxr-xr-x    1 root     root           51 Jun 16  1999 logrotate
-rwxr-xr-x    1 root     root          390 Sep 14  1999 makewhatis.cron
-rwxr-xr-x    1 root     root          459 Mar 25  1999 radiusd.cron.daily
-rwxr-xr-x    1 root     root           99 Jul 23 23:48 slocate.cron
-rwxr-xr-x    1 root     root          103 Sep 25  1999 tetex.cron
-rwxr-xr-x    1 root     root          104 Aug 30  1999 tmpwatch

 

It is advisable to go through each of these now to see what your system is doing to itself behind your back.
 
The at command
 at will execute a command at some future time. I suppose it is essential to know, although I never used it myself until writing this chapter. at is the front to the atd daemon which, like crond will almost definitely be running.
 Try our wave file example, remembering to press Ctrl-D to get the <EOT> (End Of Text):

 

 
 
 
 
5
 [root@cericon /etc]# at 14:19
at> /usr/bin/play /etc/theetone.wav
at> <EOT>
warning: commands will be executed using /bin/sh
job 3 at 2000-09-02 14:19

 

 You can the type atq to get a list of current jobs:

 
 3       2000-09-02 14:19 a

 

a means is the queue name, 3 is the job number, and 2000-09-02 14:19 is the scheduled time of execution. While play is executing, atq will give:

 
 3       2000-09-02 14:19 =

 

 The at and atd man pages contain additional information.

 

每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这 个文件中,这个文件是不可以直接编辑的,只可以用crontab -e 来编辑。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务

 

 

附图。这是本人常用的一个crontab的case。

 

  相关解决方案