1

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?

  • 1
    If 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. – Bratchley Feb 3 '15 at 21:19
  • so 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 Feb 3 '15 at 21:25
3

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.

  • 1
    Thanks 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? – neal mukherjee Feb 3 '15 at 21:03
1

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.

1

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.

0

Check permission for your crontab (/var/spool/cron/crontabs/$USER). It should be 600 and owner root:crontab. It wouldn't work otherwise. Also, of course, check crontab syntax (and line endings), scripts paths and permissions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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