perl fork doesn't work properly when run remotely (via ssh)

Whenever you launch background jobs via non-interactive ssh commands, you need to close or otherwise tie off stdin, stdout, & stderr. Otherwise ssh will wait for the backgrounded process to exit. FAQ.

This is called disassociating or detaching from the controlling terminal and is a general best practice when writing background jobs, not just for SSH.

So the simplest change that doesn't mute your entire command is to add:

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

right after your print "Output started";. If your child process needs to print output periodically during its run, then you'll need to redirect to a log file instead.


ssh [email protected] 'nohup perl script.pl'

You aren't able to exit because there's still a process attached. You need to nohup it.


What is happening is that ssh is executing 'perl script.pl' as a command directly. If you have 'screen' available, you could do:

$ ssh [email protected] 'screen -d -m perl script.pl'

to have it running on a detached screen, and reattach later with screen -r

Tags:

Unix

Perl

Ssh

Fork