LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-04-2008, 12:24 AM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
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).
 
Old 03-04-2008, 05:57 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,814

Rep: Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553Reputation: 2553
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

Last edited by chrism01; 03-04-2008 at 08:07 PM.
 
Old 03-04-2008, 08:06 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182Reputation: 3182
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.
 
Old 03-04-2008, 10:17 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
I guess I was unclear about that. I did start it with a & after. I haven't tried nohup though.
 
Old 03-04-2008, 10:28 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 03-04-2008 at 10:44 PM.
 
Old 03-04-2008, 11:56 PM   #6
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminals and Background Processes dfego Linux - General 3 11-11-2006 09:52 AM
background processes digit Linux - Software 3 07-27-2003 01:27 PM
processes in background zetsui Linux - General 8 06-14-2003 01:52 PM
Shell scripting and background processes - help needed. trafalgar Programming 3 06-08-2003 10:15 AM
background processes hakum Programming 1 04-08-2002 11:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration