Operating System - Linux
1828038 Members
2067 Online
109973 Solutions
New Discussion

Restart a function if it gets aborted during the excution of script

 
SOLVED
Go to solution
Raghu Chikkamenahalli
Frequent Advisor

Restart a function if it gets aborted during the excution of script

Hello All,

I have a shell script which runs pl/sql procedures sequentially.
For example,
The fist procedure connects to database and extract a record from the database,
Second procedure does an update to database viz. removes a row from the same database.
And during the third procedure execution, if it get aborted (because of some reasons), the script will call exit 1.
Now my concern is if I rerun the script, it will run the whole script once again. I
Please show some pointers to keep track the successfully executed functions so that when the script get executed next time it should only run the unsuccessful function which got terminated last time.
12 REPLIES 12
James R. Ferguson
Acclaimed Contributor
Solution

Re: Restart a function if it gets aborted during the excution of script

Hi:

You can write your own restart information (e.g. a last completed step number) to a file periodically. Upon beginning execution you determine if the file exists and if so read the necessary restart data and begin at the appropriate step in the overall process. Obviously, one you achieve a successful termination of your process, remove the file.

Regards!

...JRF...
Raghu Chikkamenahalli
Frequent Advisor

Re: Restart a function if it gets aborted during the excution of script

Hi JRF

Thanks much for the pointer.

Based on your inputs, I have put the controls to create a file ( which act like a lock) once the procedure exection is sucessful. Later at the end of the script I have incorporated a function to clean up all the lock files.

But I am curious to know about how store the step information which you have mentioned. If possbile can show some light on the same.

Many Thanks,
Regards,
Raghu
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again) Raghu:

> But I am curious to know about how store the step information which you have mentioned. If possbile can show some light on the same.

This really depends on what information you need. A simple integer step number might be all that you need to be able to restart at the appropriate point in your code. You could write this into a file and read it upon restart if the file was present, or you could include the numeric value in the file name itself.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Have your lock file record (one per line) a series of increasing numbers (starting at 1) as each step is completed. If the script finishes completely then rm the file.

Your script should look something like this:

typeset -i LAST_TASK=0
typeset LOCKFILE=mylock

if [[ -r "${LOCKFILE} ]]
then # file exists
LAST_TASK=$(tail -1 "${LOCKFILE}")
fi

rm -f "${LOCKFILE}"

if [[ ${LAST_TASK} -lt 1 ]]
then
# Do step 1
echo "1" >> "${LOCKFILE}"
fi

if [[ ${LAST_TASK} -lt 2 ]]
then
# Do Step 2
echo "2" >> "${LOCKFILE}"
fi

and so on until the end; if it reaches that point rm ${LOCKFILE}.


If it ain't broke, I can fix that.
Raghu Chikkamenahalli
Frequent Advisor

Re: Restart a function if it gets aborted during the excution of script

Hi JRF,

Thanks much. I will try the same.

Regards,
Raghu.
Raghu Chikkamenahalli
Frequent Advisor

Re: Restart a function if it gets aborted during the excution of script

Hi,

Thanks for sharing the code.

I will work on the same.

Regards,
Raghu.
Raghu Chikkamenahalli
Frequent Advisor

Re: Restart a function if it gets aborted during the excution of script

Hi Experts,

Based on your guidelines, achieved restart capability of a function. Here I am logging each step with a step number (which is an integer) in to file permanently. If all the steps get executed successfully, file will be cleared.

Now I have one more requirement, suppose if a step number passed as an argument, only that step should get execute.

In concise, here is a brief about the requirement.

1. If no arguments given, the script should check the step no exists in the restart file or not, if it exists, it should not get executed.

2. If a step no/nos (single/multiple) are given as an argument, it should execute only those steps, and not the entire program.


I request you to kindly suggest a pointer for the same. Your inputs are highly appreciated.
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again) Raghu:

I'd appreciate some credit for my first suggestion, too.

However, in answer to your latest question, this snippet of shell should enable you to get started. Look at the 'sh-posix' manpages too. The script below validates that any argument passed is a number.

# cat ./parse
#!/usr/bin/sh
if [ $# -eq 0 ]; then
STEP=0
echo "no arguments; use file"
else
while (( $# > 0 ))
do
STEP=$1
shift
if [ `expr "${STEP}" : '[0-9]*'` -ne `expr "${STEP}" : '.*'` ]; then
echo "ERROR: STEP must be a number; scanning: ${STEP}"
exit 1
fi
echo "STEP-${STEP} requested"
done
fi

...Thus:

# ./parse 3 5 7
STEP-3 requested
STEP-5 requested
STEP-7 requested

# ./parse 2
STEP-2 requested

# ./parse
no arguments; use file

# ./parse 2 and 4a
STEP-2 requested
ERROR: STEP must be a number; scanning: 4a

Regards!

...JRF...
Raghu Chikkamenahalli
Frequent Advisor

Re: Restart a function if it gets aborted during the excution of script

Hi JRF,

Sorry, it was omitted by an oversight. I have assigned the points now.

I will work on the same.

Regards,
Raghu
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again) Raghu:

Since the code I presented is destructive to the argument list (the 'shift') we need to preserve the original argument list and then process it, in another pass, if its elements are valid.

The following modification might be useful to you.

# cat ./parse
#!/usr/bin/sh
typeset -i MAX=$#-1
typeset -i N=0

if [ $# -eq 0 ]; then
STEP=0
echo "no arguments; use file"
else
set -A STEPS $@
while (( $# > 0 ))
do
STEP=$1
shift
if [ `expr "${STEP}" : '[0-9]*'` -ne `expr "${STEP}" : '.*'` ]; then
echo "ERROR: STEP must be a number; scanning: ${STEP}"
exit 1
fi
echo "STEP-${STEP} requested"
done
fi

N=0
while (( N <= MAX ))
do
echo "now processing STEP-${STEPS[$N]}"
let N=N+1
done
exit 0

Note that the script's value of 'MAX' is established to be zero-relative, since elements of an array ('STEPS') are referenced with zero as the first element.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

>JRF: set -A STEPS $@

If you want perfection on saving and restoring, you should quote the parms:
set -A STEPS "$@"
James R. Ferguson
Acclaimed Contributor

Re: Restart a function if it gets aborted during the excution of script

Hi (again):

Dennis>: If you want perfection on saving and restoring, you should quote the parms:
set -A STEPS "$@"

Agreed, although in the context I suggested the argument list, there would be no need to supply it in quotes like "4 5 6" 7 8 versus "4 5 6 7 8" versus 4 5 6 7 8 (without any quotes. Your point is noted, though. Thanks.

Regards!

...JRF...