I have a test.sh script
#!/bin/sh php /home/v/file.php sh /root/x/some.sh
when I execute the file as root from command line it works.
sh /home/v/test.sh
when I set it to crontab -e (is the root cron), is not working
* * * * * sh /home/v/test.sh
What do I do wrong?
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
According to the man:
The cron daemon starts a subshell
from your HOME directory. If you
schedule a command to run when you are
not logged in and you want commands
in your .profile file to run, the
command must explicitly read your
.profile file.The cron daemon supplies a default
environment for every shell, defining
HOME, LOGNAME, SHELL (=/usr/bin/sh),
and PATH (=/usr/bin).
So cron daemon doesn’t know where php is and you should specify the full php path by hand, for example (I don’t know your real PHP path):
#!/bin/sh /usr/local/bin/php /home/v/file.php sh /root/x/some.sh
Another way is to source the /etc/profile (or your .profile/.bashrc), for example
* * * * * . /home/v/.bashrc ; sh /home/v/test.sh
This is useful if your .bashrc set the environment variables that you need (i.e. PATH)
EDIT
An interesting reading is “Newbie: Intro to cron“, don’t undervalue the article from the title (It’s a reading for everybody), in fact it’s well written complete and answer perfectly to your question:
…
PATH contains the directories which
will be in the search path for cron
e.g if you’ve got a program ‘foo’ in
the directory /usr/cog/bin, it might
be worth adding /usr/cog/bin to the
path, as it will stop you having to
use the full path to ‘foo’ every time
you want to call it.
…
Method 2
There are four common causes for commands working when typed in a terminal but not from cron, in order of commonness:
- Cron provides a limited environment, e.g., a minimal
$PATH, and other expected variables missing. - Cron invokes /bin/sh by default, whereas you may be using some other shell interactively.
- Cron treats the % character specially (it is turned into a newline in the command).
- Cron doesn’t provide a terminal or graphical environment.
If your job produces any output, including error messages, cron sends you an email with the whole output. Make sure you read the mail you receive locally or forward it to an address you read. To forward mail from a local account to some other address, put the other address in ~/.forward. If the cron job is running as a system user (root, webmaster, …), make sure that user’s mail is redirected to you (and any other admin); with most mail setups, put lines like root: elzo in /etc/aliases.
Method 3
The cron daemon usually executes your command in a shell where the PATH environment variable is restricted to some system default, e.g. /usr/bin:/bin.
Probably, your php command is not available in /usr/bin or /bin and thus the script fails when executed via cron and runs successful when not.
Cron usually reports errors or job messages via a mail to the root user (i.e. when a command returns an exit status != 0 or produces output to stdout/stderr) after the job is finished.
Depending on your system you have to setup local mail delivery to get these messages.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0