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
5
  • What user shall run this script? Commented Jul 18, 2016 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) Commented Jul 18, 2016 at 14:55
  • Please, try crontab -e to verify if the command is there and add it if not. Commented Jul 18, 2016 at 14:56
  • Try adding a new line after the command. Cron might not see the command. Having */1 is redundant. * means every minute. Commented Jul 18, 2016 at 15:04
  • Originally I was running every 5 minutes, but 1 minute is to test. I'm basically using the following one-liner stackoverflow.com/a/9625233/5743740 so I'm not sure what a new line will accomplish Commented Jul 18, 2016 at 15:11

2 Answers 2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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
@ggwc So, as suggested by @Jason K, try to remove */1 leaving only *.
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.

1 Comment

congratulations, nice approach. Thanks for sharing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.