来自php脚本的Cron不起作用

时间:2016-01-29 07:36:00

标签: php linux ubuntu cron crontab

我已经通过方法 file_put_contents 在php脚本中将 / etc / crontab 文件添加了cron作业,但是这个cron没有执行。如果我将此文件保存在 nano 编辑器中,则cron正常工作。可以告知为什么php脚本添加的cron没有执行? 我尝试重新加载cron守护进程,但这没有帮助。 我在文章http://www.kavoir.com/2011/10/php-crontab-class-to-add-and-remove-cron-jobs.html中使用了cron课程。

我将方法 saveJobs 更改为保存crons到文件 / etc / crontab

static public function saveJobs($jobs = array()) {
    $output = file_put_contents('/etc/crontab', self::arrayToString($jobs));
    return $output;
}

添加cron的命令

Crontab::addJob("*/1 * * * * root php -f /var/www/get_all_clients_data_db.php ");

cron的记录被添加到crontab,但是这个cron没有执行。

/ etc / crontab中

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
*/1 * * * * root php -f /var/www/get_all_clients_data_db.php

我为错误道歉。这是我在StackOverflow上的第一个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

分享您的代码,很可能您缺少链接共享库错误。只需启用crontab日志记录并检查确切的错误。

有一个用于记录的命令行选项。输出保存到screenlog.n文件,其中n是屏幕的编号。从屏幕的手册页:

' - L'告诉屏幕打开自动输出记录。

从屏幕启动程序并检查日志未运行的原因。

/usr/bin/screen -d -m -S -L CRMService /home/CRMService

如果你进一步坚持,请告诉我!

答案 1 :(得分:0)

我发现了错误。系统日志/var/log/syslog有记录

/usr/sbin/cron[8391]: (*system*) ERROR (Missing newline before EOF, this crontab file will be ignored)

当cron被脚本添加时。我添加新行(\ n)后添加cron的方法。