I'm working with some scripts which must be executed from crontab. Some of them rewrite crontab.
in.php defines if read.php should be run by crontab. Then read.php is executed by crontab. This works fine.
When read.php is executed, it might detect that it should be not running, if this happens, it rewrites crontab to avoid its own execution, until in.php rewrites crontab again. The problem is that this second step is not working.
I have been looking for the error and I have found that my cron.txt is correctly written. However, crontab command fails and returns 127 error. But when I execute read.php manually all works fine.
Both scripts use the same functions to rewrite crontab. But this is happening just in read.php, not in in.php.
Is there a time to rewrite crontab since it executes a task? It may explain my problem, because in.php takes longer time to be executed than read.php.
Thank you!
PD: I have checked that the path to cron.txt in crontab command is the same if it is executed automatically and if I execute it manually.
EDIT: When I speak about rewriting crontab I refer rewrite a cron.txt file and to execute crontab [path]/cron.txt
Without access to your code, we can only speculate about what goes wrong, but a much simpler and more robust solution would be to add a wrapper which decides whether or not to execute the real job, and simply always run the wrapper from crontab. In the simplest possible case, execute a file only if it exists:
55 * * * * test -x hourly && ./hourly
Rename $HOME/hourly
to something else to prevent it from executing.
Or, if the conditions are simple, just make the script bail out early if the conditions are not met.
case $moon_phase in full | new ) exit 0 ;; esac