2

I'm having some problems with running the Crontab with PHP files.

I have this same php scripts running in the Crontab for more than 6 months but some days ago mysteriously they stopped to execute the code correctly. The Crontab works but the script produces an error. The other interesting thing is that the same script runs without errors in the SSH Terminal.

What should be the problem here? Is there a way to configure the Crontab to run with the same configuration of the SSH Terminal?

John
  • 215
André
  • 121
  • 1
    So, what is the error? – phunehehe Mar 22 '11 at 11:08
  • 1
    phuhenehe is right; Please edit your question to clarify what the error is -- places like [http://pastebin.com] are useful in that regard if the error becomes too large, but until and unless we see the error message we can't really help much. – Shadur-don't-feed-the-AI Mar 22 '11 at 12:06
  • Ok, I will update it in 4h. I don't have access to the error right now. I was trying to understand if the environments of the SSH Terminal and the Crontab environment are not the same. – André Mar 22 '11 at 12:19
  • there are different config files for php running from apache or from the commandine, but i pretty sure crontab uses the latter. which again leaves me kinda baffled about what might be wrong. like Shadur pointed out, the specific error would help. – kuhkatz Mar 22 '11 at 16:59
  • If you are on Debian and updated to v. 6, the default shell changed from bash to dash. Perhaps you use bash at the login shell but the cron uses dash. – franziskus Mar 23 '11 at 09:57

3 Answers3

6

The most common reason why a script works from the command line but not from a crontab is that the script depends on an environment variable. Crontabs only have a few environment variables set: typically only HOME, USER, SHELL (set to /bin/sh) and PATH (set to a system default). If you need more, you must define them in the crontab file, or source ~/.profile from the command.

Perhaps you have two versions of PHP installed, one that comes first in your command line $PATH and one that comes first in your system default $PATH, and the system default PHP changed recently. But it's impossible to make more than an educated guess since you don't say what error you're getting.

0

One more thing to not assume in a script run by cron: value of $HOME. Use $HOME explicitly, do not assume it has some particular value. Call commands and reference files by full paths.

  • Cron jobs generally do have $HOME set correctly. On my system, a cron job has $HOME, $LOGNAME, $PATH, $LANG, $SHELL, and $PWD. Still, using $HOME explicitly is a good idea. – Keith Thompson Nov 08 '11 at 21:07
0

Make sure that you crontab executes your scripts as the same user as the one you are logged in as.

Some env var or file rights has probably changed for one of the users.

Johan
  • 4,583