4

I'm trying to set up a cron job to run a PHP script daily as part of a web-based booking system. It's been running perfectly for several years on a different Apache server using this in the crontab:

0 3 * * * wget -q -O /dev/null http://www.my_other_domain.org.uk/scheduled/my_script.php

I've just installed the script on another Apache server, and it runs correctly when called through the browser. However when I try to use cron, the cron daemon gives the message:

-: warning: setlocale: LC_ALL: cannot change locale (en_GB.utf-8)
-: wget: command not found

When I tried

0 3 * * * /usr/bin/env wget -q -O /dev/null http://www.mydomain.org.uk/scheduled/my_script.php

I got the message:

-: warning: setlocale: LC_ALL: cannot change locale (en_GB.utf-8)
-: /usr/bin/env: No such file or directory

Firstly, am I right in assuming that the setlocale warning is not relevant to the problem?

I've also tried

0 3 * * * /usr/bin/php /var/www/vhosts/mydomain/httpdocs/scheduled/my_script.php

which gives

-: /usr/bin/php: No such file or directory

I also tried putting a shebang as the first line of my script, and getting cron to call it using the full path. The cron daemon reported that there was no such file or directory as /[full_path]/my_script.php .

Trying to use curl was no better.

I haven't got shell access to the server and I've been setting up the cron job through the Plesk control panel. The cron job seems to be running as scheduled. The system administrator did the following for me:

#which php
/usr/bin/php

#which wget
/usr/bin/wget

#which curl
/usr/bin/curl

That seems to confirm that the commands are in the right location.

The above is an abbreviated history - I've lost count of the permutations and combinations I've tried, mostly as a result of reading advice in this and other forums.

Can anyone tell me why the cron job is unable to find the commands and suggest a solution?

closed as off-topic by jasonwryan, Jeff Schaller, GAD3R, sam, countermode Jan 9 '17 at 8:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Jeff Schaller, GAD3R, sam, countermode
If this question can be reworded to fit the rules in the help center, please edit the question.

13

Your shell knows where to find executables by looking in your PATH, which is set by your environment. cron does not share the same environment. Adding this at the top of the crontab should tell it where to find commands as you expect:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  • 5
    +1 -- Inability of cron to find commands that one expects it to almost always boils down to $PATH issues. If you want to, try a cron entry like * * * * * echo $PATH – a CVn Feb 20 '13 at 15:21
  • Thanks Michael - your suggested cron entry returned `/usr/bin:/bin' – user32706 Feb 21 '13 at 11:20
  • This worked for me. Why doesn't it include my path by default? I don't see any explanation in the man pages. – sudo Jan 5 '17 at 18:52
  • @sudo How would cron know what "your path" is? There are a million and one ways that your environment and PATH could be composed, cron can't guess how you might end up producing it, not to mention that your PATH may differ for different programs running as your user. – Chris Down Jan 7 '17 at 22:29
  • @ChrisDown The same way any shell I open in my user defines PATH and other envvars. For example, Bash would source /etc/profile, ..., ~/.bashrc. – sudo Jan 8 '17 at 8:29
2

Going by the symptoms you report, this goes beyond a broken PATH: that wouldn't affect /usr/bin/env. However, it's difficult to be sure because your reporting is inconsistent. Did you write /usr/bin/env or usr/bin/env?

Make sure that there are only ASCII characters in your crontab, no unbreakable spaces or non-printing characters. Also, check that you haven't defined some environment variables that may be causing trouble, such as PATH or LD_LIBRARY_PATH.

If /usr/bin/env: No such file or directory strongly suggests that there is a misconfiguration on the server that you cannot fix. The virtual environment or security framework on the server isn't allowing you to run commands, at least from cron. It's impossible to know what could be wrong without knowing what kind of environment the server is running. This is something that the system administrator must fix.

The setlocale error is not the cause of the problem but may well be another symptom.

  • Thanks, Gilles - there was a typo in my original message. I definitely wrote /usr/bin/env . I'll pass your comments on to the system administrator. – user32706 Feb 21 '13 at 11:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.