Bash脚本手动执行正常运行但不使用crontab

发布于 2024-12-01 06:48:15 字数 1322 浏览 2 评论 0原文

你好,我有一个像这样的脚本:

#!/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 -

它基本上在与该脚本相同的目录中执行 jar 文件,然后 tar 结果,gzip 它并执行另一个 bash 文件,然后删除所有以前收集的文件。问题是我需要每天运行这个脚本,并且我使用 crontab 来执行此操作。它仍然返回空 tar 文件,但如果我手动执行它,它工作得很好。我还有其他 4 个脚本在 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷血 2024-12-08 06:48:15

我会尝试一下:你的脚本是由 /bin/sh 而不是 /bin/bash 运行的。

尝试在 cron 条目中使用 bash 显式运行它,如下所示:

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

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
吖咩 2024-12-08 06:48:15

我猜测当您执行 $JAVACMD -jar LatencyCounter.jar LatencyCounter.xml $DATE $HR 时,您不在包含 LatencyCounter.jar 的目录中。在进入 for 循环之前,您可能需要cd $CURDIR

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文