3

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
ugotchi
  • 1,003
  • 1
  • 12
  • 32
  • What user shall run this script? – JrBenito Jul 18 '16 at 14:51
  • it'll be hosted on a linux apache webserver. but right now the crontab doesn't run even for my current user (using OSX) – ugotchi Jul 18 '16 at 14:55
  • Please, try `crontab -e` to verify if the command is there and add it if not. – JrBenito Jul 18 '16 at 14:56
  • Try adding a new line after the command. Cron might not see the command. Having */1 is redundant. * means every minute. – Jason K Jul 18 '16 at 15:04
  • Originally I was running every 5 minutes, but 1 minute is to test. I'm basically using the following one-liner http://stackoverflow.com/a/9625233/5743740 so I'm not sure what a new line will accomplish – ugotchi Jul 18 '16 at 15:11

2 Answers2

2

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.
JrBenito
  • 973
  • 8
  • 30
  • when I run `(crontab -u daemon -l 2>/dev/null; echo "*/1 * * * * php ./ProcessCycleTimeData.php") | crontab -` manually, I can see it when I type `crontab -l` so it's in there. it just doesn't run for some reason. I'm using the same account to run the ProcessCycleTimeData.php from the command line, and it runs without having to sudo – ugotchi Jul 18 '16 at 15:14
  • @ggwc So, as suggested by @Jason K, try to remove `*/1` leaving only `*`. – JrBenito Jul 18 '16 at 16:31
2

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.

ugotchi
  • 1,003
  • 1
  • 12
  • 32