来自 php 脚本的 Cron 不工作

Cron from php script not work

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

我更改了方法 saveJobs 以将 cron 保存到文件 /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

对于错误,我深表歉意。这是我在 Whosebug 上的第一个问题。 提前致谢。

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

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

‘-L’告诉屏幕打开自动输出日志。

从屏幕启动您的程序并检查日志为什么它不是 运行。

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

如果你还坚持,请告诉我!

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

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

当 cron 被脚本添加时。在我向添加 cron 的方法添加新行 (\n) 后,它执行了。