简体   繁体   中英

php execute crontab script every 1 minute

as part of a php-slim web application, in my init.php file I require a Crontab.php which contains the following code:

<?php

// clears any existing crontab jobs first
exec("crontab -r");

$ctCommand = '"*/1 * * * * php ./ProcessCycleTimeData.php"';
exec("(crontab -l 2>/dev/null; echo " . $ctCommand . " ) | crontab -");

exec("crontab -l");
?>

When I run the commands manually, the job gets added and I can see it being recorded, however it doesn't seem to run. However, when I run php ./ProcessCycleTimeData.php it works fine. Any ideas where to troubleshoot this?

I'm looking into the error logs, and every minute I get the following log:

crontab: no crontab for daemon

You can use crontab -e to edit the crontab, this will open your default editor (generally vi if other is not set).

Edit the crontab for the user you need this script to run, and add a line as:

*/1 * * * * php ./ProcessCycleTimeData.php

This means

Every one minute

  • Note: The PHP snippet you provide is trying to edit the crontab and add the above line. However it might be failing due lack of permission.

I managed to get it working. My solution was to check if the crontab was actually running by appending the crontab job with >>/tmp/auto-update.log 2>&1 which allowed me to further investigate the issue.

I found that the crontab was indeed running, but as a different user (hence why when I was manually calling crontab -e I could not see the job since I am calling it as my own username.

The crontab was also actually invoking my PHP script, where I could then find out the errors in the auto-update.log , which happened to be due to incorrectly stating the require paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM