1

I am running two mysqlsp in a bash script. I want to start both of them simultaneously (they are running in remote servers) and put them in background, once completed proceed to next step. I want to get the exit status of both(to know in case error happened). With this in mind I have tried below with sleep command, but not working.

Editing as per Jeff Schaller's answer : Below are what I have tried.

givinv@87-109:~$ sleep5 & PID5=$!; exit5=$?; sleep 4 & PID4=$!; exit4=$?; wait $PID5 $PID4; echo $exit5 $exit4
[1] 29530
[2] 29531
No command 'sleep5' found, did you mean:
 Command 'sleep' from package 'coreutils' (main)
sleep5: command not found
[1]-  Exit 127                sleep5
[2]+  Done                    sleep 4
0 0
givinv@87-109:~$ unset exit5 exit4
givinv@87-109:~$ sleep5 & exit5=$?;PID5=$!; sleep 4 & exit4=$?;PID4=$!; wait $PID5 $PID4; echo $exit5 $exit4
[1] 29535
[2] 29536
No command 'sleep5' found, did you mean:
 Command 'sleep' from package 'coreutils' (main)
sleep5: command not found
[1]-  Exit 127                sleep5
[2]+  Done                    sleep 4
0 0
givinv@87-109:~$ 

I knowingly put sleep5(error command), but still getting 0 as exit status. When I run above commands, I see a line like [1]- Exit 127 sleep5. Is it possible to trap that while running from script?

How to get the exit status of two commands that run simultaneously?

1

From the bash man page, my italicized emphasis:

wait

wait [-n] [jobspec or pid …]

Wait until the child process specified by each process ID pid or job specification jobspec exits and return the exit status of the last command waited for.

I think you've perhaps misplaced your backgrounding symbol &:

sleep 5 ;exit5=$? & PID5=$!;wait $PID5; echo $?; echo $exit5

Says to sleep for 5 seconds; once that's done, set the (zero) exit code into exit5 as a background process; set the job ID of that assignment into PID5; then wait for that assignment to finish; the assignment finished successfully, so echo shows that zero; then the last echo shows a blank line, as the background process (subshell) has exited, leaving the exit5 variable unassigned (empty).

Perhaps this is the situation you were going for?

sleep 5 & PID5=$!; wait $PID5; exit5=$?; echo $exit5

Put sleep 5 into the background, saving its PID into PID5. Wait for the process to exit, then capture its return code into exit5, then echo the saved return code.

  • Thanks for correcting it. But when I tried to run two sleep commands simultaneously, it took the total amount(I tried sleep 4 and sleep 5 and I want to run that in a total pf 5 seconds and NOT in 9 seconds). sleep 4 & PID4=$!; wait $PID4;exit4=$?; sleep 5& PID5=$!; wait $PID5;exit5=$?; echo $exit5 ; echo $exit4 – prado Jan 30 '17 at 12:39
  • Because you're waiting between the two of them. Wait for both at the end. – Jeff Schaller Jan 30 '17 at 12:48
  • well, the time is corrected, but not getting proper exit status. I tried both below. sleep5 & PID5=$!; exit5=$?; sleep 4 & PID4=$!; exit4=$?; wait $PID5 $PID4; echo $exit5 $exit4 and sleep5 & exit5=$?;PID5=$!; sleep 4 & exit4=$?;PID4=$!; wait $PID5 $PID4; echo $exit5 $exit4. I knowingly put sleep5(error command), but still getting 0 as exit status. PS: When I run above commands, I see a line like [1]- Exit 127 sleep5. Is it possible to trap that while running from script? – prado Jan 30 '17 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.