Launching And Detaching

H

Hal Vaughan

I'm using one simple program to launch some daemons. When I launch a
program, I use backticks. (This is all on Linux, in bash, btw.) I've
added "2>&1 >/dev/null &" to the command line, so it's like this:

`/path/to/program 2>&1 >/dev/null &`;

I've noticed that if I include the '&' so the program detaches from the
shell, if I kill the launching program, the child process survives. I've
also tried this by forking first:

if (!fork()) {
`/path/to/program 2>&1 >/dev/null &`;
exit;
}

I had the hope that if I forked and launched like this, once all the
programs were launched, the launching program would exit. It doesn't. Is
there a way I can launch a program, then have the first program exit, so
it's no longer using that bash instance (without killing it)?

Thanks!

Hal
 
J

John W. Krahn

Hal said:
I'm using one simple program to launch some daemons. When I launch a
program, I use backticks. (This is all on Linux, in bash, btw.) I've
added "2>&1 >/dev/null &" to the command line, so it's like this:

`/path/to/program 2>&1 >/dev/null &`;

I've noticed that if I include the '&' so the program detaches from the
shell, if I kill the launching program, the child process survives. I've
also tried this by forking first:

if (!fork()) {
`/path/to/program 2>&1 >/dev/null &`;
exit;
}

I had the hope that if I forked and launched like this, once all the
programs were launched, the launching program would exit. It doesn't. Is
there a way I can launch a program, then have the first program exit, so
it's no longer using that bash instance (without killing it)?

Just putting '&' at the end does not detach a process from the shell.

man nohup
perldoc perlipc


John
 
A

axel

Hal Vaughan said:
I'm using one simple program to launch some daemons. When I launch a
program, I use backticks. (This is all on Linux, in bash, btw.) I've
added "2>&1 >/dev/null &" to the command line, so it's like this:

`/path/to/program 2>&1 >/dev/null &`;

I've noticed that if I include the '&' so the program detaches from the
shell, if I kill the launching program, the child process survives. I've
also tried this by forking first:

if (!fork()) {
`/path/to/program 2>&1 >/dev/null &`;
exit;
}

I had the hope that if I forked and launched like this, once all the
programs were launched, the launching program would exit. It doesn't. Is
there a way I can launch a program, then have the first program exit, so
it's no longer using that bash instance (without killing it)?

You should not been using backticks for this purpose - they
are designed to collect data from the external program
and will wait for that program.

Have a look at: perldoc -f exec

Axel
 
H

Hal Vaughan

You should not been using backticks for this purpose - they
are designed to collect data from the external program
and will wait for that program.

Have a look at: perldoc -f exec

Axel

A good idea. I was just reading up on it and didn't even know about that
function. Thanks!

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top