Can I execute a multiline command in Perl's backticks?

醉酒当歌 提交于 2019-12-12 18:35:22

问题


In Unix, I have a process that I want to run using nohup. However this process will at some point wait at a prompt where I have to enter yes or no for it to continue. So far, in Unix I have been doing the following

nohup myprocess <<EOF
y
EOF

So I start the process 'myprocess' using nohup and pipe in a file with 'y' then close the file. The lines above are effectively three seperate commands - i.e. I hit enter on the first line in UNIX, then I get a prompt where I enter 'y' and then press enter to then finally type 'EOF' and hit return again.

I want to know execute this in Perl but I am not sure how I can execute this command as it is over three lines. I don't know if the following will work....

my $startprocess = `nohup myprocess <<EOF &
y
EOF
`

Please help - thank you!


回答1:


I think your proposal will work as is. If not, try replacing the redirect with a pipe:

my $startprocess = `(echo "y" | nohup myprocess) &`;

Also, depending on WHY you are doing a nohup, please look at the following pure Perl daemonizing approach using Proc::Daemon : How can I run a Perl script as a system daemon in linux?




回答2:


Expect for interactive programs can be used as well.



来源:https://stackoverflow.com/questions/3835809/can-i-execute-a-multiline-command-in-perls-backticks

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