简体   繁体   中英

Bash script manual execute run normally but not with crontab

Hello i have a script like this one:

#!/usr/bin/bash


ARSIP=/apps/bea/scripts/arsip
CURDIR=/apps/bea/scripts
OUTDIR=/apps/bea/scripts/out
DIRLOG=/apps/bea/jboss-6.0.0/server/default/log
LISTFILE=$CURDIR/tmp/file.$$
DATE=`perl -e 'use POSIX; print strftime "%Y-%m-%d", localtime time-86400;'`

JAVACMD=/apps/bea/jdk1.6.0_26/bin/sparcv9/java

HR=00

for (( c=0; c<24; c++ ))
do
    echo $DATE $HR
    $JAVACMD -jar LatencyCounter.jar LatencyCounter.xml $DATE $HR
    sleep 1
    cd $OUTDIR
        mv btw_120-180.txt btw_120-180-$DATE-$HR.txt
        mv btw_180-360.txt btw_180-360-$DATE-$HR.txt
        mv btw_60-120.txt btw_60-120-$DATE-$HR.txt
        mv failed_to_deliver.txt failed_to_deliver-$DATE-$HR.txt
        mv gt_360.txt gt_360-$DATE-$HR.txt
        mv out.log out-$DATE-$HR.log
    cd -
    let HR=10#$HR+1
        HR=$(printf %02d $HR);
done

cd $OUTDIR

tar -cf latency-$DATE.tar btw*-$DATE-*.txt gt*$DATE*.txt out-$DATE-*.log
sleep 300
gzip latency-$DATE.tar
sleep 300
/apps/bea/scripts/summaryLatency.sh
sleep 300
rm -f btw* failed* gt* out*

#mv latency-$DATE.tar.gz ../$ARSIP
cd -

It basically execute jar files in same directory as this script and then tar the result, gzip it and execute another bash file then delete all of the previous collected files. The problem is i need this script to run daily and i use crontab to do that. It still return empty tar file but if i execute it manually it works well..I also have other 4 scripts running in crontab and they work good..i still can't figure out what is the main reason of this phenomena

thank you

I'll take a stab: your script is run by /bin/sh instead of /bin/bash.

Try explicitly running it with bash at the cron entry, like this:

* * * * * /bin/bash /your/script

I'm guessing that when you execute $JAVACMD -jar LatencyCounter.jar LatencyCounter.xml $DATE $HR , you're not in the directory containing LatencyCounter.jar. You might want to cd $CURDIR before you enter the for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM