Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am facing a weired problem. I wrote a bash script to get the status of an app. Script is running fine when i am executing it from shell but when I am trying to execute the same script from crontab its not working. Not sure what is happening here...

Both manual and cron are running from root account.

share|improve this question
    
possible duplicate of crontab to run bash script (ssh command in it) not working –  Dennis Williamson Dec 6 '10 at 15:27
    
Did you look at /var/log/crond ? –  shaktale Dec 6 '10 at 16:10

3 Answers 3

up vote 3 down vote accepted

This is generally because cron does not enable the PATH the same way as the shell does. Try typing "echo $PATH" at a prompt. Take the resulting PATH string and add it to the top of crontab file as PATH= That will probably fix it.

Mark

share|improve this answer
    
Thanks Man! that worked. That did not came in my mind. I was trying to add more verbosity to the script to fix it. –  Ramesh Kumar Dec 6 '10 at 14:38

Root is probably getting an e-mail message explaining the error. Check root's mail, or else set MAILTO to some other address at the top of the file.

In addition to the possibility of a different PATH, be aware that crontab has some slightly weird escaping rules. Particularly, % becomes newline, and characters after the first % become stdin to the command.

share|improve this answer

@mfarver already answered I just want to add:
Any script running at startup will no have the PATH env variable set. You will have to call all those commands you use with the full path unless you export PATH. Anytime you are creating one simply add export PATH=$PATH:/bin:/usr/bin:/usr/local/bin at the beginning and you should be fine. Of course if you need other folders in that path add them there. I didnt want to make this an answer since @mfarver has the correct one already but I didn't have enough points to simply add a comment :).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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