Linux 脚本与 if/else
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的 Shell 是 Bash:
使用 $?
$? 是包含上一个命令的返回码的变量(如果成功则为 0)
)和||
因此,您可以尾部执行的行或命令:&& 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)
Using && and ||
So you can tail the executed line or command with : && echo "cron job successful" || echo "cron job failed"
如果您可以依赖 Java 命令的退出状态,那么只需编写:
请注意,输出将邮寄给您(至少在某些系统上),或者丢失到
/dev/null
。这些变量使一切都更适合 SO 屏幕;我可能也会在常规脚本中使用它们,尽管这将是一个边缘决定。如果您不能依赖退出状态,则必须执行其他操作来确定它是否成功,但无论如何,一般原则都非常相同。
If you can rely on the exit status of the Java command, then simply write:
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.