Cron的用法

centos_logo_45.pngLinux下可以用Crontab设置 定时启动的任务。这个功能很有用,可以定时每天或每周运行一个程序完成特定的任务,比如Dailybuild等等 。

简单介绍用法如下:

1. 编辑定时启动的任务:

crontab -e

添加一行如下:

0  1 * * * /usr/bin/php /myaccount/test.php

其意义是定时每天凌晨一点,运行test.php。还可以更灵活的设置每月或每周运行的任务。

*     *   *   *    *  command to be executed
-     -    -    -    -
|     |     |     |     |
|     |     |     |     +—– day of week (0 – 6) (Sunday=0)
|     |     |     +——- month (1 – 12)
|     |     +——— day of month (1 – 31)
|     +———– hour (0 – 23)
+————- min (0 – 59)

2.邮件功能

缺省情况下,cron任务执行完后,会给运行cron任务的Linux帐号发一个邮件。

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

3. 转发Cron邮件

如果想用外部邮件帐户接受Cron邮件,这就需要使用sendmail的转发功能。可以编辑当前用户的.forward,并且添加外部邮件地址。

It is possible to automatically forward your Linux e-mail by creating a file called .forward in your homedirectory. The contents should be the e-mail adress that you want to forward the mail to, for example:

anotheremail@example.org

This will only work if the access permissions of your home directory and the .forward do not include group and other writable bits. Perform the following commands to be sure:

chmod go-w $HOME
chmod go-w $HOME/.forward

本文由IT Farmer的博客创作,欢迎转载并保留对本博的链接。 Tags:,

Leave a Reply