Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Restart a job in Shell ?

Status
Not open for further replies.
Oct 22, 2001
215
US
This is what I got:
I would appreciate if any one can help on this.... Thanks

I got 50 scripts, I want to run one by one , using shell. Some thing like
this:
sh job1
sh job2
sh job3
and so on

Now I want to exit when a job stop/error occured, say, job3 had the problem.
After I fix the problem, when I restart, I want to start at job3 again not from the beginning. Any idea a better way to do this?
 
have each job write a return code to a temporary file.
check the temporary file after each job run for errors.

sh job1;echo $? > /tmp/tmpfile.$$

CODE=`cat /tmp/tmpfile/$$`
if [ $CODE -gt 0 ]
then
echo "Error encountered!"
# do some action here
fi



Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Thanks but that does not answer my question... I am trying to avoid using another temp/file. I want to use one shell script. Can this be done?
 
It's not a trivial task and if you want to avoid using a temp file I think you may be disappointed but yes - it can be done. There would be some limitations though.

You'll need a controlling script that can restart your shell script at various points in the shell script. The shell script would need to record its progress so that, if it failed, the controlling script could then restart it. It could do this by reading the shell script and creating a new shell script that only contained lines that still needed to be run.

Restart wouldn't be possible at every point however, look at this:

1 if [[ some_condition ]]
2 then
3 if [[ another_condition ]]
4 then
5 do_something
6 fi
7 fi

8 task1
9 if [[ yet_another_condition ]]

10 then
11 another_task
12 fi

13 last_task

Restart would only be possible on the lines coloured green. Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks... I agree with you. I only have one task per script/job. I was thinking to automated if this is possible:

counter =1
For i = counter to 50
'create a shell script to do the job that is call job1
'echo job ($1) and make a file called job($counter)
'if possible run this/exicute it
'if ran okay:
set counter =counter +1
over write this entire file/shell and set the counter to the current counter
Next i

One reason I am doing an over write because, when I re start, I don't want it call job1 say if it failed on job3.

Is there a better way? IF not, can my steps are duable in shell? Thanks a lot...
 
You can use a script like the following.
In this script, each job is executed with the "Step" function.
Call the script with the restart step number as argument.

Restart <-- Execute starting at the first step
Restart 2 <-- Execute starting from step 2


--- Restart ---

CurrentStep=0;
RestartStep=${1:-0}

Step()
{
let CurrentStep=CurrentStep+1
if [ $CurrentStep -ge $RestartStep ]
then
if [ $CurrentStep -eq $RestartStep ] ; then
echo &quot;-- Restart step reached&quot;
fi
echo &quot;-- Execute step $CurrentStep&quot;
$@
Status=$?
if [ $Status -ne 0 ]
then
echo &quot;-- Error in step $CurrentStep&quot;
echo &quot;-- Restart with : $0 $CurrentStep&quot;
exit $Status
fi
else
echo &quot;-- Bypass step $CurrentStep&quot;
fi
}

Step job1 # step 1
Step Job2 # step 2
Step Job3 # step 3

--- End of Restart --- Jean Pierre.
 
Ok, that's really neat - I like *that* Jean Pierre :) Mike
&quot;Experience is the comb that Nature gives us after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Thank very much Jean... It is making some sence now...I just want to ask a few question, since my experience is limited in this aea...

IS this restart function can go inside my shell script?
How do I call the script with the restart step #? Can you be little detail? Thanks a lot again...
 
Question again, how to call this restart function from my Shell script? Can any one help?
 
Restart is an example of what can your script could be.

In your own script add :
CurrentStep=0;
RestartStep=${1:-0}
Step()
{
. . . . . .
}

and start all your jobs with the Step function :
Step job1
. . . . .
Step jobn

If the name of your script is &quot;ExecAll&quot; for example,
you can restart the execution at step 2 with :
ExecAll 2
Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top