0

I need to start a process in the background, then continue and run other processes. I want to exit the script as soon as my myBackgroundProcess process ends with non 0 exit code. (or soon ish when i check in the loop).

#!/usr/bin/env bash
set -e

nohup myBackgroundProcess &
myPid=$!


for i in {1..30000}; do
   sleep 10
   someProcessDoingStuff
   wait ${myPid}  #this is blocking, but i want to continue the loop
done

so I would like some type of non blocking way to check the exit code of a process by its pid, if it has finished.

its is close this issue WAIT for "any process" to finish but not 100% same, however the issue gives a clue as to how one might implement your own nonblocking wait

A possible work around

$(sleep 10 ; echo $? > exitCode.txt) &

any better solutions, or issues with the above. 1 issue is 1 loose the std out of the process.

so i want to start a process in the background, keep its std out err, and have the exit code written to a file. with out modding the process its self.

Community
  • 1
  • 1
dmc
  • 401
  • 4
  • 14
  • i found this https://lists.gnu.org/archive/html/bug-bash/2010-09/msg00005.html – dmc Jul 01 '16 at 09:24
  • I would make `myBackgroundProcess` to create a file on exit and check existence of that file (you can put the exist status into the file). – GMichael Jul 01 '16 at 09:32
  • I like the idea of writing the exit code to file, and checking for the file existing. – dmc Jul 01 '16 at 09:50
  • a possible implementation of your idea $(sleep 10 ; echo $? > exitCode.txt) & – dmc Jul 01 '16 at 09:58
  • issue: I want the std out also – dmc Jul 01 '16 at 10:01
  • What is you `myBackgroundProcess `? – GMichael Jul 01 '16 at 10:06
  • a jvm, controlling, other jvm's on remote boxes via activeMQ jms – dmc Jul 01 '16 at 10:07
  • Your Java process has to create that file and put the exit code into it. Your bash script has to monitor the existence of that file and read the code from it. No standard input/output need to be involved – GMichael Jul 01 '16 at 10:10
  • I would like a way, where the backprocess dose not need to be modified, I am sure its possible, – dmc Jul 01 '16 at 10:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116178/discussion-between-gmichael-and-dmc). – GMichael Jul 01 '16 at 10:18

0 Answers0