Linux 脚本与 if/else

发布于 2024-12-22 13:44:10 字数 566 浏览 3 评论 0原文

我正在 ubuntu 服务器中通过运行 java 文件的 Cron 执行日常脚本。我希望它包含在 if/else 块中,这样如果 java 文件执行成功,它应该发送一条回显消息“成功”,如果失败,它应该发送一条回显消息“失败”。

以下是脚本 updateGroupScores.sh 的现有行,

java -classpath "/home/ubuntu/live/build/WEB-INF/lib/*:/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF/classes/" com.generalsentiment.update.UpdateGroupScores > /var/gs/livecron/crongroupsentimentscores.log

我想修改它们,例如 -

if (java file runs successfull)
then
echo "cron job successfull"

else
then 
echo "cron job failed"

-- 谢谢

I am executing a daily script in ubuntu server via Cron which runs a java file. I want to it to be included in if/else block such that if the java file is executed successfully its should send an echo message "successfull" and if it fails it should send an echo message "failed".

Here are the existing lines for the script updateGroupScores.sh

java -classpath "/home/ubuntu/live/build/WEB-INF/lib/*:/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF/classes/" com.generalsentiment.update.UpdateGroupScores > /var/gs/livecron/crongroupsentimentscores.log

i want to modify them something like -

if (java file runs successfull)
then
echo "cron job successfull"

else
then 
echo "cron job failed"

--
Thanks

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

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

发布评论

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

评论(2

心奴独伤 2024-12-29 13:44:10

假设您的 Shell 是 Bash:

使用 $?

  • $? 是包含上一个命令的返回码的变量(如果成功则为 0)

    java -classpath "/home/ubuntu/live/build/WEB-INF/lib/*:/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF /classes/" com.generalsentiment.update.UpdateGroupScores > /var/gs/livecron/crongroupsentimentscores.log
    
    如果 [[ $? == 0]]
    然后
        echo "cron作业成功"
    别的
        echo "cron 作业失败"
    菲
    

)和||

  • && 如果前一个成功,则执行以下命令
  • || 如果前一个不成功,则执行以下命令

因此,您可以尾部执行的行或命令:&& echo "cron 作业成功" || echo“cron作业失败”

Assuming your Shell is Bash:

Using $?

  • $? is the variable that contains the return code of the previous command (0 if successful)

    java -classpath "/home/ubuntu/live/build/WEB-INF/lib/*:/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF/classes/" com.generalsentiment.update.UpdateGroupScores > /var/gs/livecron/crongroupsentimentscores.log
    
    if [[ $? == 0 ]]
    then
        echo "cron job successful"
    else
        echo "cron job failed"
    fi
    

Using && and ||

  • && makes the following command to be executed if previous is successful
  • || makes the following command to be executed if previous is not successful

So you can tail the executed line or command with : && echo "cron job successful" || echo "cron job failed"

白云不回头 2024-12-29 13:44:10

如果您可以依赖 Java 命令的退出状态,那么只需编写:

C1="/home/ubuntu/live/build/WEB-INF/lib/*"
C2="/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF/classes/"
LOG="/var/gs/livecron/crongroupsentimentscores.log"

if java -classpath "$C1:$C2" com.generalsentiment.update.UpdateGroupScores > "$LOG"
then echo "cron job successful"
else echo "cron job failed"
fi

请注意,输出将邮寄给您(至少在某些系统上),或者丢失到 /dev/null。这些变量使一切都更适合 SO 屏幕;我可能也会在常规脚本中使用它们,尽管这将是一个边缘决定。

如果您不能依赖退出状态,则必须执行其他操作来确定它是否成功,但无论如何,一般原则都非常相同。

If you can rely on the exit status of the Java command, then simply write:

C1="/home/ubuntu/live/build/WEB-INF/lib/*"
C2="/var/lib/tomcat7/lib/*:/home/ubuntu/live/build/WEB-INF/classes/"
LOG="/var/gs/livecron/crongroupsentimentscores.log"

if java -classpath "$C1:$C2" com.generalsentiment.update.UpdateGroupScores > "$LOG"
then echo "cron job successful"
else echo "cron job failed"
fi

Note that the output will be mailed to you (on some systems at least), or lost to /dev/null. The variables make everything fit better on the SO screen; I'd probably use them in a regular script too, though it would be a borderline decision.

If you can't rely on the exit status, you'll have to do something else to decide whether it succeeded or not, but the general principle is very much the same regardless.

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