Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle error/exception in shell script?

Below is my script that I am executing in the bash. And it works fine.

fileexist=0
for i in $( ls /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done); do
  mv /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done /data/read-only/clv/daily/archieve-wip/
  fileexist=1
done

Problem Statement:-

In my above shell script which has to be run daily using cron job, I don't have any error/exception handling mechanism. Suppose if anything gets wrong then I don't know what's has happened?

As after the above script is executed, there are some other scripts that will be dependent on the data provided by above script, so I always get's complaint from the other people who are depending on my script data that something wrong has happened.

So is there any way I can get notified if anything wrong has happened in my script? Suppose if the cluster is having some maintenance and at that time I am running my script, so definitely it will be failing for sure, so can I be notified if my above scripts failed, so that I will be sure something wrong has happened.

Hope my question is clear enough.

Any thoughts will be appreciated.

like image 287
arsenal Avatar asked Sep 08 '12 23:09

arsenal


2 Answers

You can check for the exit status of each command, as freetx answered, but this is manual error checking rather than exception handling. The standard way to get the equivalent of exception handling in sh is to start the script with set -e. That tells sh to exit with a non-zero status as soon as any executed command fails (i.e. exits with a non-zero exit status).

If it is intended for some command in such a script to (possibly) fail, you can use the construct COMMAND || true, which will force a zero exit status for that expression. For example:

#!/bin/sh

# if any of the following fails, the script fails
set -e
mkdir -p destdir/1/2
mv foo destdir/1/2
touch /done || true    # allowed to fail

Another way to ensure that you are notified when things go wrong in a script invoked by cron is to adhere to the Unix convention of printing nothing unless an error ocurred. Successful runs will then pass without notice, and unsuccessful runs will cause the cron daemon to notify you of the error via email. Note that local mail delivery must be correctly configured on your system for this to work.

like image 151
user4815162342 Avatar answered Oct 07 '22 01:10

user4815162342


Its customary for every unix command line utility to return 0 upon success and non-zero on failure. Therefore you can use the $? pattern to display the last return value and handle things accordingly.

For instance:

> ls
> file1 file2
> echo $?
> 0
> ls file.no.exist
> echo $?
> 1

Therefore, you can use this as rudimentary error detection to see if something goes wrong. So the normal approach would be

some_command
if [ $? -gt 0 ]
then
handle_error here
fi
like image 21
freetx Avatar answered Oct 07 '22 01:10

freetx