0

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

2 Answers 2

1

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"

Sign up to request clarification or add additional context in comments.

1 Comment

You can simply test the exit status of the command with if; all that the various test commands ([ and [[ for example) do is check a condition and exit with the correct status. Once upon a long time ago, the test command was not a shell built-in (and if it was invoked as [ it required its last argument to be ]) and was literally executed. Nowadays, it is a built-in, but the same basic idea applies. So, you can write if cmd1 ... ; then echo this; else echo that; fi.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.