简体   繁体   中英

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.

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 (ie 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 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.

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

well if other scripts are on the same machine, then you could do a pgrep in other scripts for this script if found to sleep for a while and try other scripts later rechecking process is gone.

If script is on another machine or even local the other method is to produce a temp file on remote machine accessible via a running http browser that other scripts can check status ie running or complete

You could also either wrap script around another that looks for these errors and emails you if it finds it if not sends result as per normal to who ever

go=0;
function check_running() {

         running=`pgrep -f your_script.sh|wc -l `
    if [ $running -gt 1 ]; then
        echo "already running $0 -- instances found $running ";
      go=1;
}

check_running;
if [ $go -ge 1 ];then
      execute your other script
else
   sleep 120;
    check_running;
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM