英文:
PHP script running directly but not as cron
问题
我正在使用php8.1-fpm
。编写了一个连接到IMAP的脚本,然后执行某些操作并记录日志条目。当我像这样直接运行脚本时,我可以在日志文件中看到条目。
php /var/myscripts/index.php
但是当我将其作为cron作业运行时,日志中什么都没有被写入。我打开了cron作为sudo crontab -e
,然后写入了以下内容:
* * * * * php /var/myscripts/index.php
我已经检查了syslog
,cron正在运行,但我的PHP脚本没有写入日志。以下是syslog
的记录:
Jul 27 15:20:01 My-VM CRON[74081]: (root) CMD (php /var/myscripts/index.php)
我已登录为sudo用户,而index.php
也由相同的用户拥有。
我做错了什么?
英文:
I am using php8.1-fpm
. Wrote a script that connects to IMAP, then does something and logs entries in a log file. When I run the script directly like this then I can see entry in log file.
php /var/myscripts/index.php
But when running it as cron then nothing is written in logs. I opened cron as sudo crontab -e
and then wrote the following:
* * * * * php /var/myscripts/index.php
I have checked syslog
and cron is running but nothing is written in logs by my PHP script. Here is syslog
entry:
Jul 27 15:20:01 My-VM CRON[74081]: (root) CMD (php /var/myscripts/index.php)
I am logged in sudo user and index.php
is owned by same user.
What am I doing wrong?
答案1
得分: 2
在通过cron运行脚本时,它可能会使用与您在命令行中使用的不同的PHP二进制文件。为确保使用正确的PHP二进制文件,请在cron作业中指定PHP二进制文件的绝对路径。您可以通过在终端中运行which php命令来查找PHP的路径。
* * * * * /usr/bin/php /var/myscripts/index.php
在您的cron作业中,将标准输出和错误都重定向到日志文件。这将帮助您捕获脚本生成的任何错误或消息。将您的cron作业修改如下:
* * * /usr/bin/php /var/myscripts/index.php >> /var/myscripts/cron.log 2>&1
您可以检查任何错误或调试消息,这可能有助于您解决问题。
英文:
When running a script via cron, it may use a different PHP binary than the one you use in the command line. To ensure that the correct PHP binary is being used, specify the absolute path to the PHP binary in your cron job. You can find the path to PHP by running the which php command in your terminal.
* * * * * /usr/bin/php /var/myscripts/index.php
In your cron job, redirect both standard output and errors to a log file. This will help you capture any errors or messages generated by your script. Modify your cron job like this:
* * * /usr/bin/php /var/myscripts/index.php >> /var/myscripts/cron.log 2>&1
you can examine any errors or debug messages that might help you troubleshoot the issue.
答案2
得分: -2
<?php
ignore_user_abort(true);
set_time_limit(0);
do{
/*
jobs script
*/
usleep(500*1000);// 0.5 milliseconds
}while(true);
英文:
crontab or crontask have a lot of problems, i prefer to use php script directly as cron.
<?php
ignore_user_abort(true);
set_time_limit(0);
do{
/*
jobs script
*/
usleep(500*1000);// 0.5 milliseconds
}while(true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论