wait example

Hi Gurus,

Some questions regarding wait. I have tried searching in this forum for threads on wait but not completely got what I am looking for.

Background:
One script (.sh) that starts/calls a reference to an application's executable and submits a batch job to it. Objective is to wait for the submitted job and once the batch job finishes, get its return code to the scheduler which is sniffing for a return code.

parent script: run.sh

  #! /usr/sh
  #sample to understand behavior
   /opt/app/r.exe /code/project1/foo.r &
    wait
   echo $?

I get invalid file /code/project1/foo.r 0

I man(ed) wait read thru it but frankly the WIFEXITED, WNOHANG etc I understood but could not find a simple workable "example". The forum threads talk about spawning multiple scripts form one parent script, or ftp, etc etc which is not what I could use.

Questions:

  1. Does wait only works with a script starting a child script?
  2. I google (d) but could not find any simple working example of wait or an example of how best to use the WIFEXITED, WNOHANG, etc etc. Is there a dummy proof simple example?

Perhaps this is rather simple but I have been staring at it for a while now and dazed :wall:

All suggestions are eagerly accepted and thank you in advance.

---------- Post updated at 06:40 PM ---------- Previous update was at 06:36 PM ----------

Hi Gurus,

Some questions regarding wait. I have tried searching in this forum for threads on wait but not completely got what I am looking for.

Background:
One script (.sh) that starts/calls a reference to an application's executable and submits a batch job to it. Objective is to wait for the submitted job and once the batch job finishes, get its return code to the scheduler which is sniffing for a return code.

parent script: run.sh

  #! /usr/sh
  #sample to understand behavior
   /opt/app/r.exe /code/project1/foo.r &
    wait
   echo $?

I get invalid file /code/project1/foo.r 0

I man(ed) wait read thru it but frankly the WIFEXITED, WNOHANG etc I understood but could not find a simple workable "example". The forum threads talk about spawning multiple scripts form one parent script, or ftp, etc etc which is not what I could use.

Questions:

  1. Does wait only works with a script starting a child script?
  2. I google (d) but could not find any simple working example of wait or an example of how best to use the WIFEXITED, WNOHANG, etc etc. Is there a dummy proof simple example?

Perhaps this is rather simple but I have been staring at it for a while now and dazed :wall:

All suggestions are eagerly accepted and thank you in advance.

** I googled, did man wait, did more google. Perhaps it needs to be dummied down a bit for me, lol.

Sounds like you are getting the manual entry on the C function wait if manual entry has (3) or (3C) following it that is the C library routine not the OS command which has (1).

Wait is a builtin for most shells so you will find details about in in man sh or man bash

  1. Yes you can only wait for you own child processes.
  2. To get the exist status of the command you need to explicitly wait for it PID. exit status of wait is 127 if it fails to find the specified child process.

Example:

background_cmd &
BPID=$!
wait $BPID
stat=$?
 
if [ $stat -eq 127 ] 
then
    echo "It's already finished"
else
    echo "Exit status was $stat"
fi

Wow! What was taking me ages you took couple of minutes. No wonder I am a big fan of knowledge. I thank you sincerely for your time and advice.

Awesome and it worked.

Best,

RS