ruby

领取游戏新手卡来http://5433.com

导航

linux定时执行

 

crontab -e

就会打开一个编辑窗口,第一行会有内容格式的提示

 

# m h  dom mon dow   command

crontab的范例格式: 
下面是c r o n t a b的格式: 
分< >时< >日< >月< >星期< >要运行的命令 
其中< >表示空格。

crontab条目举例 
这里有c r o n t a b文件条目的一些例子: 
30 21* * * /apps/bin/cleanup.sh 
上面的例子表示每晚的2 1 : 3 0运行/ a p p s / b i n目录下的c l e a n u p . s h。 
45 4 1,10,22 * * /apps/bin/backup.sh 
上面的例子表示每月1、1 0、2 2日的4 : 4 5运行/ a p p s / b i n目录下的b a c k u p . s h。 
10 1 * * 6,0 /bin/find -name "core" -exec rm {} ; 
上面的例子表示每周六、周日的1 : 1 0运行一个f i n d命令。 
0,30 18-23 * * * /apps/bin/dbcheck.sh 
上面的例子表示在每天1 8 : 0 0至2 3 : 0 0之间每隔3 0分钟运行/ a p p s / b i n目录下的d b c h e c k . s h。 
0 23 * * 6 /apps/bin/qtrend.sh 
上面的例子表示每星期六的11 : 0 0 p m运行/ a p p s / b i n目录下的q t r e n d . s h。 

命令形式: 
c r o n t a b命令的一般形式为: 
Crontab [-u user] -e -l -r 
其中: 
-u 用户名。 
-e 编辑c r o n t a b文件。 
-l 列出c r o n t a b文件中的内容。 
-r 删除c r o n t a b文件。 
如果使用自己的名字登录,就不用使用- u选项,因为在执行c r o n t a b命令时,该命令能够 
知道当前的用户 

 

注意:在完成编辑以后,要重新启动cron进程:

可能会出现:Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory

sudo apt-get install dbus
然后在重启

~# sudo /etc/init.d/cron restart

 

编辑crontab文件

crontab位于/ect/文件夹,在http://wiki.ubuntu.org.cn/CronHowto上有关于它的详细介绍,但是我看的不是太懂。

打开crontab文件,如果没有编辑过可以看到如下类似的内容:

  1. # /etc/crontab: system-wide crontab
  2. # Unlike any other crontab you don't have to run the `crontab'
  3. # command to install the new version when you edit this file
  4. and files in /etc/cron.d. These files also have username fields,
  5. # that none of the other crontabs do.
  6. SHELL=/bin/sh
  7. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  8. # m h dom mon dow user  command
  9. 17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
  10. 25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
  11. 47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
  12. 52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

由于对脚本的认知有限,不能详细解释每个命令的含义。在第10行,同样定义了文件内容的格式。可以看到比使用crontab -e命令时,多了一个user。它表示了执行命令的用户,如果是root,就表明是系统用户。于是,我加了如下一行:

3 * * * * root /home/meng/hello.sh

然后保存文件。

这时需要的注意是,要重新启动系统,修改才能起作用。(只重启cron守护进程可以吗?)如果以前使用crontab -e进行了修改,建议先注释掉,不然就不知道是哪个方法在起作用了。

当然,结果也在意料之中,脚本定时顺利执行。



posted on 2010-12-26 01:24  最初的模样  阅读(416)  评论(0编辑  收藏  举报