Re: shell scripting help

  • From: Enrique Fernandez-Pampillon <oracle.pampillon@xxxxxxxxx>
  • To: Oracle-L@xxxxxxxxxxxxx
  • Date: Fri, 5 Aug 2005 10:03:20 +0200

Hello everybody first of all I'm sorry for the long and for my
english. I think all things said related to this subject are not 100%
correct.

I'm trying to clarify how to work with backgroud processes in shell.


- The only way to execute a process in background mode is adding "&"
at the end of the sentence.
- nohup command is used for deattach the process from a terminal, i.e
you close your connection (telnet, X, ...) and the process will keep
alive.
  It's not necessary to used nohup command in a shell scripts that
will be executed with nohup, crontab, at, ...
  
A shell like:

p1&
p2&
p3&
wait
i1

it seems to be ok because of it executes p1 and p2 and p3 in
background and it waits to finish three process before execute i1.
But for me it has very important bugs: 
   - This code doesn't control any error, it excutes i1 whether p1 and
p2 and p3 are ok or whether all or one of them do not terminate ok.
   - The stdout of p1 and p2 and p2 are mixed up.

The command "wait" waits untill all processes haved terminated but it
"always" return 0.
The command "wait pid1 ... pidN" waits until pid1, ..., pidN processes
have terminated and it returns the return status of the pidN process.

Let's do me some examples:

    mymachine:/usr/users/myuser> (sleep 40 && exit 1) &
    [1]     1060183
    mymachine:/usr/users/myuser> sleep 40 &
    [2]     1181744
    mymachine:/usr/users/myuser> wait
    [2] +  Done                    sleep 40 &
    [1] +  Done(1)                 (sleep 40 && exit 1) &
    mymachine:/usr/users/myuser> echo $?
    0

It seems to have a good behaviour.

    mymachine:/usr/users/myuser> sleep 40 &
    [1]     1462190
    mymachine:/usr/users/myuser> (sleep 40 && exit 1) &
    [2]     1171882
    mymachine:/usr/users/myuser> wait
    [2] +  Done(1)                 (sleep 40 && exit 1) &
    [1] +  Done                    sleep 40 &
    mymachine:/usr/users/myuser> echo $?
    0

Ummmm, the last process executed returns 1 but the wait process returns 0

    mymachine:/usr/users/myuser> sleep 40 &
    [1]     1192834
    mymachine:/usr/users/myuser> (sleep 40 && exit 1) &
    [2]     1092754
    mymachine:/usr/users/myuser> wait 1092754 1192834
    mymachine:/usr/users/myuser> echo $?
    0

Be careful, in spite of the fact the pid "1192834" has started before
the wait returns its return status.

    mymachine:/usr/users/myuser> sleep 40 &
    [1]     1169544
    mymachine:/usr/users/myuser> (sleep 40 && exit 1) &
    [2]     1221012
    mymachine:/usr/users/myuser> wait 1169544 1221012
    mymachine:/usr/users/myuser> echo $?
    1

This is the correct behaviour.

In my expertise I recommend the following:

Sql template:

whenever sqlerror exit sql.sqlcode rollback --- or commit
whenever oserror exit failure rollback --- or commit 
...
... sql code
...
exit sql.sqlcode

To execute sqlplus use:

sqlplus -L user/pass@tns @sqlscript

An example of shell scripts (in korn):

p1 1>$TMPDIR/p1_$$.log 2>&1 &
Pid1=$!
p2 1>$TMPDIR/p2_$$.log 2>&1 &
Pid2=$!
p3 1>$TMPDIR/p3_$$.log 2>&1 &
Pid3=$!
typeset -i TotalErros=0
for n in 1 2 3; do # In this case I have 3 forks
   wait $(eval echo $"Pid${n}")
   ret_status=$?
   cat p${n}_$$.log
   if [ "${ret_status}" != "0" ]; then
      echo "ERROR - Process p${n} have terminated with ${ret_status}"
      TotalErros=$((${TotalErros}+1))
   else
      echo "OK    - Process p${n} have terminated with ${ret_status}"
   fi
done
if [ ${TotalErrors} -ne 0 ]; then
   ### ERRROR
else
   ### OK
fi

Be carefuly if you use csh because of it has the wait function built
in and is possible different behaviour.

HTH

Enrique

On 8/4/05, oracle-l-bounce@xxxxxxxxxxxxx <oracle-l-bounce@xxxxxxxxxxxxx> wrote:
>                Hi Listers,
> 
>                I have a korn shell script which needs to run 3 sqlplus
> scripts in parallel then run the final sqlplus script.  Is there a way
> to ensure that sqlplus scripts 1 thru 3 completes before running the
> final sqlplus script?  I am a newbie in shell scripting.
> 
>   use wait (a shell builtin):
> 
> foo1 &
> foo2 &
> wait
> foo3
> --
> //www.freelists.org/webpage/oracle-l
> 


-- 
------------------------------------------------
Enrique
--
//www.freelists.org/webpage/oracle-l

Other related posts: