Adi
Adi

Reputation: 75

Crontab for script

My script is under /u01/software/aditya/script/ directory. Name of script is myscript.sh. I am able to run this script and getting output too. I am trying to set a cronjob for this script at 6.30 daily morning. I am doing this as root user. I have done following steps but not getting output.

crontab -e
30 06 * * * sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log
:wq

but not getting any update in hello.log file :( . please help….

Upvotes: 0

Views: 162

Answers (2)

G. Grasso
G. Grasso

Reputation: 111

Normally this is caused by a PATH problem. There is a very good chance that myscript.sh calls a command that is not available in the PATH that cron runs with. Some options to fix this are:

  • Make sure that every command in myscript.sh is a full path-reference (tedious)
  • Add source ~/.bashrc to the top of myscript.sh
  • Add export PATH=$PATH:<colon delimited list of paths necessary for myscript.sh to run correctly>

Pick one of the above, or you could also choose one of the options here: Hourly cron job did not run

Upvotes: 0

scai
scai

Reputation: 21469

First check your cron log file which is usually in /var/log/syslog. There should be entries similar to

Sep 17 06:30:01 localhost CRON[17725]: (root) CMD (sh /u01/software/aditya/script/myscript.sh >> /u01/software/aditya/hello.log)

If not, your script has never been run. This could be due to a broken crontab file. You should make sure that this file always ends with a newline, better insert more than one at the end so that deleting one accidentally won't break the file.

If this line exists in the log file then your script has been run, but didn't generate any output. This can happen due to a different environment when being run via cron.

Also note that >> only redirects stdout, not stderr. If you want to redirect stderr too, then add 2>&1 at the end of the line.

Upvotes: 1

Related Questions