我已通过 file_put_contents 方法将 cron 作业添加到 php 脚本中的 /etc/crontab 文件中,但该 cron 未执行。如果我将此文件保存在 nano 编辑器中,cron 就可以工作。能否提示一下为什么php脚本添加的cron没有执行? 我尝试重新加载 cron 守护进程,但这没有帮助。 我使用文章 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
对于错误,我深表歉意。这是我在 StackOverflow 上的第一个问题。 提前致谢。
最佳答案
分享您的代码,很可能您缺少链接共享库错误。只需启用 crontab 日志记录并检查出现的确切错误。
有一个用于日志记录的命令行选项。输出保存到 screenlog.n 文件中,其中 n 是屏幕编号。从 screen 的手册页:
‘-L’告诉屏幕打开自动输出日志记录。
从屏幕启动程序并检查日志,了解其未运行的原因。
/usr/bin/screen -d -m -S -L CRMService /home/CRMService
如果您进一步陷入困境,请告诉我!
关于php - php 脚本中的 Cron 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35079137/