遇到问题:终端关了,我的后台程序也关了,如何将后台程序不随终端关闭

遇到问题:终端关了,我的后台程序也关了,如何将后台程序不随终端关闭

如题
nohup
package Daemonize;
use POSIX;
use strict;

use Exporter;
BEGIN {
    use Exporter ();
    use vars qw(@ISA @EXPORT);
    @ISA = qw(Exporter);
    @EXPORT = qw( &daemonize)
}

my $logFile = "/tmp/daemon.log";    # Log requests and errors to this file

my %children;                        # Store pids of children


sub daemonize {

    my $pid = fork;    # Fork off the main process

    defined ($pid) or die "Cannot start daemon: $!";     
    # If no PID is defined, the daemon failed to start


    print "arent daemon running.\n" if $pid;
    # If we have a PID, the parent daemonized okay


    exit if $pid;
    # Return control to the user


   # Now we're a daemonized parent process!


       OSIX::setsid();
    # Become a session leader


    close (STDOUT);
    # Close file handles to detach from any terminals

    close (STDIN);
    #close (STDERR);

    open STDERR,">>/tmp/daemon.error";

    # Set up signals we want to catch.

    #Let's log warnings, fatal errors, and catch hangups and dying children


    $SIG{__WARN__} = sub {
        &logMessage ("NOTE! " . join(" ", @_));
    };

    $SIG{__DIE__} = sub {
        &logMessage ("FATAL! " . join(" ", @_));
        exit;
    };

    $SIG{HUP} = $SIG{INT} = $SIG{TERM} = sub {
        # Any sort of death trigger results in instant death of all

      my $sig = shift;
        $SIG{$sig} = 'IGNORE';
        kill 'INT' => keys %children;
        die "killed by $sig\n";
        exit;
      };    

    $SIG{CHLD} = \&REAPER;
}

sub logMessage {
    my $message = shift;
    (my $sec, my $min, my $hour, my $mday, my $mon, my $year) = gmtime();
    $mon++;
    $mon = sprintf("%0.2d", $mon);
    $mday = sprintf("%0.2d", $mday);
    $hour = sprintf("%0.2d", $hour);
    $min = sprintf("%0.2d", $min);
    $sec = sprintf("%0.2d", $sec);
    $year += 1900;
    my $time = qq{$year/$mon/$mday $hour:$min:$sec};
    open (FH, ">>" . $logFile);
    print FH $time . " - " . $message;
    close (FH);
}

sub REAPER {
    my $pid;

    $pid = waitpid(-1, &WNOHANG);

    if ($pid == -1) {
        # no child waiting.  Ignore it.

    } elsif (WIFEXITED($?)) {
        logMessage "rocess $pid exited.\n";
    } else {
        logMessage "False alarm on $pid.\n";
    }
    $SIG{CHLD} = \&REAPER;          # in case of unreliable signals

}

1;
忽略sighup信号就行了.
nohup u_command &
让你的程序后台运行不就行了
nohup perl $0 &
那如果我需要再把这个后台进程调回到前台呢,怎么实现
unix系统基础不好,去shell版看看,就答案了!
nohup /root/shell/script.sh &
jobs
fg
bg