LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Scripting and background processes (https://www.linuxquestions.org/questions/programming-9/scripting-and-background-processes-625528/)

PatrickNew 03-04-2008 12:24 AM

Scripting and background processes
 
I'm trying to write an application which needs to work with a Gtk::Socket/Plug (gtkmm's wrapper around the XEmbed protocol). In order to test this, I need to run another application which will embed in. I have written a very simple such plug app with gtkmm. I am currently launching it by calling a shell script synchronously, and this shell script calls the function in the background. Yes, there are design reasons for doing this, the script is itself a stub for a much more advanced script.

My trouble is this. I call the script from my main application via glibmm's wrapper around execvp, and it runs, and it launches my child application, but the shell script will not end while the child is still running. It appears in a ps listing as defunct, and since I am calling synchronously, it halts my main application, preventing the embedding from occurring.

Is there a way to instruct a bourne shell script called in this way to *not* wait on any backgrounded children? I know that it was backgrounded, because later parts of the script run (it touches a file, so I have evidence that later commands were executed).

chrism01 03-04-2008 05:57 AM

The usual way is:

/somedir/myprog &

the '&' means run prog in background and continue wihout waiting.
To ensure it keeps running even if you logout, use:

nohup /somedir/myprog &

the 'nohup' dates back to the old days, it's short for no-hangup (the phone line).
These days it effectively means detach the prog from the terminal, so you can logout without breaking it.
Note that '&' backgrounds a process, but it's still attached to the terminal.

HTH

sundialsvcs 03-04-2008 08:06 PM

A parent-process is supposed to wait for its child to end. This is "by default, by design." The comments about & and nohup are entirely apropos, and should need no further explanation.

PatrickNew 03-04-2008 10:17 PM

I guess I was unclear about that. I did start it with a & after. I haven't tried nohup though.

ta0kira 03-04-2008 10:28 PM

You can also use setsid to separate the process from its parent entirely. That sounds like what you need. Or just use () around the & line:
Code:

#!/bin/bash

( sleep 2 && echo "boom!" & )

wait
echo "parent exit!"
exit 0

ta0kira

PatrickNew 03-04-2008 11:56 PM

Thanks, setsid did exactly the trick! Now it's just down to debugging the rest of the C++ code. :-)


All times are GMT -5. The time now is 01:55 AM.