Shell – Cronjob doesn’t execute shell script, but when I am executing it standalone it works. Why

cronkshshellshell-script

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?

Best Answer

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.

Related Question