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 07-03-2005, 01:21 PM   #1
p0rtius
LQ Newbie
 
Registered: Jul 2005
Location: italy
Distribution: gentoo
Posts: 4

Rep: Reputation: 0
bash assignments of commands substitution


Hi all,
This is my first post in linuxquestions.org, nice to meet you
I'm italian so sorry for my macaronic english
Here my problem.

*******************************
#include <unistd.h>

int main()
{
int pid;
pid = fork();
if (pid == 0)
{
sleep(600);
exit(0);
}
else
{
printf("PID: %d", pid);
exit(0);
}
}
*******************************

This is a method to run processes as daemons in background: parent fork creates a child and when it exits(the parent) INIT inherits the child. In this example the child sleeps for 600 seconds, and his parent prints the child pid and then exits.
In a bash script i would want to save into a variable the string printed by the parent, so i can do something like:

(1) VAR=`./daemon`

into $VAR i should have the pid of the daemon launched into the background.

The problem is that running "./daemon" from a login shell it's ok: parent launches the child and then returns. Instead, if I use a bash assignment, like (1), bash doesn't return but wait the child ends, as if it was in foreground, and it seems in foreground, infact it's keyboard signals sensitive: CTRL-C affect the child, that should be into the background.

This behavior is smashing my brain . Any idea ?
Thank you in advance
Regards
 
Old 07-03-2005, 02:17 PM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
That *IS* the way it is supposed to be, as far as I know. The command substitution is supposed to collect all output, so it is getting the PID from the parent, and waiting for output from the child, I believe.
 
Old 07-03-2005, 02:47 PM   #3
p0rtius
LQ Newbie
 
Registered: Jul 2005
Location: italy
Distribution: gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
Do u think there are no ways to do this ?
To save the pid in a temp file, would be a very bad idea for my app.
 
Old 07-03-2005, 02:48 PM   #4
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You could try a double fork, that will usually fully detach the child process.
 
Old 07-03-2005, 02:55 PM   #5
p0rtius
LQ Newbie
 
Registered: Jul 2005
Location: italy
Distribution: gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
Do you mean somthing like this?
Code:
int main()
{
        int pid;
        pid = fork();
        if (pid == 0)
        {
                pid = fork();
                if (pid == 0)
                {
                        sleep(600);
                        exit(0);
                }
                else
                {
                        exit(0);
                }
        }
        else
        {
                printf("PID: %d", pid);
                exit(0);
        }
}
...it doesnt'work =(
 
Old 07-03-2005, 03:50 PM   #6
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
You should also close stdin, stdout, stderr (or maybe just some, I tested will all ) in the child before the sleep, like:

fclose(stdin);
fclose(stdout);
fclose(stderr);

(of course you must include stdio.h)

I don't know exactly why, but I assume it is depends on the way the backstick operator is implemented in bash.

I came across this idea by doing an "ps -l" and saw that my bash was in pipe_wait state (so something wasn't closed...)
 
Old 07-03-2005, 07:41 PM   #7
p0rtius
LQ Newbie
 
Registered: Jul 2005
Location: italy
Distribution: gentoo
Posts: 4

Original Poster
Rep: Reputation: 0
Yhea! It works
And it works just closing the stdout.
However it's a strange behavior, and more strange that bash waits only for stdout, stderr is ignored.

Thanks a lot
 
  


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
bash - review assignments tarja Programming 4 11-24-2005 02:12 PM
Bash Process Substitution joshholt Programming 4 10-11-2005 04:15 AM
bash commands davatar Slackware 2 04-06-2005 04:05 AM
Bash commands PhuckFonix Linux - Newbie 2 03-30-2004 08:13 PM
Bash commands wprescott Linux - Distributions 6 04-27-2002 03:56 PM

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

All times are GMT -5. The time now is 09:56 AM.

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