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()
I've used Proc::Daemon with good results myself. But if you want to run something in the background and still be able to reattach to it and use it, use the screen utility. It's readily available for lots of operating systems.
--
naChoZ
Where in the nursery rhyme does it say Humpty Dumpty is an egg?