| Category: | utility scripts |
| Author/Contact Info | StephenCarville stephen@totalflood.com |
| Description: | Detaches itself from the terminal and starts a program. Orgianally written to start java code that refused to go into the background. Since then I've used it on perl 'daemons' as well. Based in a trick I learned way back in my OS-9 days. |
#!/usr/bin/perl -w
# detach from the terminal, go into the BG and then exec a program
# detacher.pl <program> {parm1} {parm2} ...
use strict;
sub main {
my ($pid,$line);
my (@args);
# fork and exit if parent
$pid = fork;
if($pid) { exit 0; }
# duplicate STD paths
open (DUPOUT,">&STDOUT");
open (DUPERR,">&STDERR");
open (DUPIN, "<&STDIN");
# close STD paths so parent can exit clenaly
close STDOUT;
close STDERR;
close STDIN;
# restore the STD paths from duplicates
open (STDOUT,">&DUPOUT");
open (STDERR,">&DUPERR");
open (STDIN,"<&DUPIN");
# avoid leaks! close teh duplicate paths
close DUPOUT;
close DUPERR;
close DUPIN;
# construct a command list
@args = ();
foreach $line (@ARGV) {
push @args,$line;
}
# finally exec the new program
exec @args;
}
main()
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: detacher.pl
by Tanktalus (Canon) on Apr 29, 2005 at 13:56 UTC | |
by enemyofthestate (Monk) on Apr 29, 2005 at 14:01 UTC | |
|
Re: detacher.pl
by naChoZ (Curate) on May 01, 2005 at 01:47 UTC |