#!/usr/bin/perl -w # detach from the terminal, go into the BG and then exec a program # detacher.pl {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()