Perl fork does not work properly on remote start (via ssh)

I have a perl script, script.pl that makes fork at startup, the parent process outputs the pid to a file, and then exits while the child process outputs something to STOUT, and then the loop goes into it for some time.

$pid = fork();

if ( ! defined $pid ) 
{
    die "Failed to fork.";
}
#Parent process
elsif($pid)
{
    if(!open (PID, ">>running_PIDs"))
    {
        warn "Error opening file to append PID";
    }
    print PID "$pid  \n";
    close PID;
}
#child process
else
{
    print "Output started";

    while($loopControl)     
    {
           #Do some stuff
    }
}

This works fine when I call it local, i.e.: perl script.pl.

The script prints some things and then returns control back to the shell. (while the child process goes into its loop in the background).

However, when I call it through an ssh control, it never returns back to the shell (nor does the "Start Record" line appear).

namely: $ ssh username@example.com 'perl script.pl'

However, the interesting thing is that the child process starts (I can see it when I type ps).

Can anyone explain what is happening?

EDIT:

I ran it under debugging and got the following:

### Forked, but don’t know how to create a new TTY.

Since the two debuggers are fighting for the same TTY, the input is very confused.

I know how to switch output to another window in xterms and OS / 2. For a manual switch, enter the name of the created TTY in $ DB :: fork_TTY or define the function DB :: get_fork_TTY () that returns this.

On UNIX-like systems, you can get the TTY name for this window by typing tty and disconnecting the shell from TTY by sleeping 1,000,000.

+5
source share
3 answers

, ssh, stdin, stdout & STDERR. ssh . FAQ.

, SSH.

, , - :

#close std fds inherited from parent
close STDIN;
close STDOUT;
close STDERR;

print "Output started";. , .

+9

ssh username@example.com 'nohup perl script.pl'

, . nohup .

+4

What happens is that ssh runs "perl script.pl" as a command directly. If you have a “screen”, you can do:

$ ssh username@example.com 'screen -d -m perl script.pl'

so that it works on a separate screen and reconnects using the -r screen

+2
source

All Articles