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?
2
Answers
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.
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:
you can examine any errors or debug messages that might help you troubleshoot the issue.