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

。_饼干妹妹 提交于 2019-12-05 10:59:57

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 username@example.com '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 username@example.com 'screen -d -m perl script.pl'

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!