Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use POSIX; # required for setsid
- # Do not wait for child processes to complete
- $SIG{CHLD} = 'IGNORE';
- # fork (and make sure we did!)
- defined (my $kid = fork) or die "Cannot fork: $!n";
- if ($kid) {
- return (1, $kid);
- }else {
- # chdir to /, stops the process from preventing an unmount
- chdir '/' or die "Can't chdir to /: $!";
- # dump our STDIN and STDOUT handles
- open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
- open STDOUT, '>/dev/null' or die "Can't write to /dev/null: $!";
- # redirect for logging
- open STDERR, '>', $log_filename or die "Can't write to log: $!";
- # Prevent locking to apache process
- setsid or die "Can't start a new session: $!";
- # execute the command
- exec( $cmd, @args );
- die "Failed to exec";
- }
- close STDIN;
- close STDOUT;
- close STDERR;
- setsid();
- $SIG{CHLD} = 'IGNORE';
- # This should flush stdout.
- my $ofh = select(STDOUT);$| = 1;select $ofh;
- my $kpid = fork;
- if ($kpid)
- {
- # Parent process
- waitpid($kpid, 0);
- }
- else
- {
- close STDIN;
- close STDOUT;
- close STDERR;
- setsid();
- my $gpid = fork;
- if (!$gpid)
- {
- open(STDIN, "</dev/null") ;#or print DEBUG2 "can't redirect stdinn";
- open(STDOUT, ">/dev/null") ;#or print DEBUG2 "can't redirect stdoutn";
- open(STDERR, ">/dev/null") ;#or print DEBUG2 "can't redirect stderrn";
- # Child process
- exec($pgm, @execargs) ;# or print DEBUG2 "exec failedn";
- }
- exit 0;
- }
Add Comment
Please, Sign In to add comment