Cronjob doesn't execute shell script, but when I am executing it standalone it works. Why?
Solution 1
Items in crontab execute with a limited environment. At the top of your script you can specify your PATH, for example,
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
or alternatively you can call each executable with its absolute path.
Solution 2
First of all, for debugging purposes, redirect the stderr to some file as well. This way you'll know what goes wrong.
15 * * * * /bin/ksh /wls_domains/resMGT/logs/bea/wlr3queuetransaction.sh 2>LOG_FILE > /wls_domains/resMGT/logs/bea/data/script.log
Next if it isn't a file location issue, take a look at this question, as it is very similar: how to set crontab PATH variable.
Solution 3
Your script does cat wlr3queue.txt
etc.... where are those files to be found? When you're trying by hand you're running ./wlr3queuetransaction.sh
but from cron you're calling with the complete pathname. Cron will run your command from the crontab's owner's home directory; presumably the files you're accessing in your script aren't in that home directory.
Always ensure that a script will work from whatever directory it's called from, meaning that you either always use complete pathnames for files / directories or that you first do a cd
into the correct directory.
Presumably you did get email messages with the error output from the script; those should have given you clues about what was wrong.
Related videos on Youtube
neal mukherjee
Updated on September 18, 2022Comments
-
neal mukherjee over 1 year
Shell Type:
>echo $SHELL /bin/ksh
Cronjob :
15 * * * * /bin/ksh /wls_domains/resMGT/logs/bea/wlr3queuetransaction.sh > /wls_domains/resMGT/logs/bea/data/script.log
The script is as below:
##log files and lookup file "wlr3queue.txt" are in the same path of the script ##/wls_domains/resMGT/logs/bea/wlr3queuetransaction.sh Script : #!/bin/ksh dt=`date +%Y-%m-%d` xy=`date|awk '{print $4}'|awk -F":" '{print $1}'` yz=`expr $xy - 1` if [ $yz -le 9 ] then yz=0$yz fi if [ $xy -gt 0 ] then for i in `cat wlr3queue.txt` do sum=0 count=`gzgrep -i "$dt" admin_resMGT_access.log* | grep "$yz:[0-5][0-9]" | grep "$i" | wc -l` if [ $count -gt 0 ] then name=`echo $i | cut -d'/' -f3` gzgrep -i "$dt" admin_resMGT_access.log* | grep "$yz:[0-5][0-9]" | grep "$i" | awk '{print $8}' > consumed_time.txt for j in `cat consumed_time.txt` do j_ms=`echo "$j * 1000" | bc` echo $j_ms >> consumed_ms_time.txt done for k in `cat consumed_ms_time.txt` do sum=`echo "$sum + $k" | bc` done avg=`echo "scale=2; ($sum/$count) "| bc` min=`cat consumed_ms_time.txt | sort -n | head -1` max=`cat consumed_ms_time.txt | sort -n | tail -1` else avg=0 min=0 max=0 fi ( echo $yz","$count","$avg","$min","$max )>>/wls_domains/resMGT/logs/bea/data/$name$dt.csv rm -f consumed_ms_time.txt done else echo "script won't execute at this hour" > temp.txt fi
I have executed the following commands and the script ran successfully.
./wlr3queuetransaction.sh sh -x wlr3queuetransaction.sh /bin/ksh /wls_domains/resMGT/logs/bea/wlr3queuetransaction.sh /bin/ksh/ wlr3queuetransaction.sh.
How to debug? What to do?
-
Bratchley over 9 yearsIf it works from the shell but not
cron
it's almost always an environmental variable that gets lost and that variable is almost always$PATH
. -
neal mukherjee over 9 yearsso adding a line PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin after the #!/bin/ksh should solve the issue I guess
-
-
neal mukherjee over 9 yearsThanks will try tommorrow with the same and let you know the result. Just to Clarify PATH means you are telling me to specify the path of the script right?