Skip to content
Advertisement

How can I make a ksh script terminate itself if any issues?

I have written a few ksh scripts, about 6 scripts. These are written to handle huge data files, something like 207 MB big. while running the script, sometimes it gets stuck and does not end. Human interruption is required. In production environment, I want it to run automatically, and should be able to end automatically if any issues without the need of any human interruption. If there are some issues with a file, the script should end and start executing the next file. How can make it terminate itself, if it gets stuck?

Advertisement

Answer

I assume, that the only way you see the issues is that the script takes too long. In that case a simple script that kills the process after a time-out should be sufficient:

#!/bin/bash
# Killersrcipt
PID=$1
TIME=$2

typeset -i i
i=0

while [ $i -lt  $TIME ] ; do
  if ps $PID > /dev/null ; then
        i=$i+1
        sleep 1
  else
        exit 0
  fi
done

kill $PID

Your workflow would then be something like:

#!/bin/bash
process_1 &
killerscript $! 60
process_2 &
killerscript $! 30
...

If you have other ways to detect issues in your processes, you can easily add them to the loop in your killerscript.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement