Discontinuing a long running script

Looking at a bash script I wrote yesterday I see that I did not give the user a way of discontinuing the processing's long run. The user's only resort was to kill the process. Killing a long-running script is never good action. Where was the script in it's process coordination? Is clean-up needed? If so, from where and how? It would be better to let the script exit at a good discontinuation point.

To this end, I would like to write the script
. Logger.sh
. Continuance.sh

CONTINUANCE=$(continuance $(basename $0))

for f in ...
do
if [ -e $CONTINUANCE ]
then
info Processing $f
...
else
warn Process discontinued before completion
exit 1
fi
done
or
. Logger.sh
. Continuance.sh

C=$(continuance $(basename $0))

while [ -e $CONTINUANCE ]
do
...
done

if [ ! -e $CONTINUANCE ]
then
warn Process discontinued before completion
fi
Where the call to "continuance" creates a temporary file, sets up a HUP trap to delete the file, and prints the instructions

2008-08-11 13:58:26 INFO To discontinue processing
send "kill -HUP 31788" or remove the file
/tmp/u-continuance-ITexo31793
The function is very simple
function continuance {
f=$(mktemp -p /tmp $1-continuance-XXXXXXXXXX)
trap "rm -f $f" HUP
info To discontinue processing send \"kill -HUP $$\" or remove the file $f
echo $f
}

No comments: