skip to Main Content

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


  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.

    Login or Signup to reply.
  2. <?php
        ignore_user_abort(true);
        set_time_limit(0);
        do{
            /*
            jobs script 
            */
            usleep(500*1000);// 0.5 milliseconds
        }while(true);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search