bash 在后台运行的命令行脚本进入停止状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17912137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line script run in background goes in stopped state
提问by Lorenzo Marcon
I have a short php utility script, I run it from cli simply with:
我有一个简短的 php 实用程序脚本,我只需使用以下命令从 cli 运行它:
php myscript.php
The script is always running, periodically performing some tasks (not relevant for the question). It doesn't need any input from the user.
After running it, I usually press CTRL+zand then run bg
to put the process in background, and everything is fine.
该脚本始终在运行,定期执行一些任务(与问题无关)。它不需要用户的任何输入。运行后,我一般按CTRL+z然后运行bg
,把进程放到后台,一切正常。
If I run it as:
如果我运行它:
php myscript.php &
the script is put on background on start, but it is also put in a stopped state. Example:
该脚本在启动时置于后台,但它也处于停止状态。例子:
[1] 11513
[1]+ Stopped php myscript.php
even running bg
at this point doesn't help, I have to run fg
, then CTRL+zand bg
again to make it work.
即使此时运行bg
也无济于事,我必须运行fg
,然后CTRL+z并bg
再次运行才能使其工作。
This is the php script:
这是php脚本:
<?
while(true){
echo 'hi '.time()."\n";
sleep(30);
}
?>
My problem is that I cannot run it directly in background, because the system stops it, and I don't understand why. How can I fix this?
我的问题是我不能直接在后台运行它,因为系统停止了它,我不明白为什么。我怎样才能解决这个问题?
update:
更新:
I made a bash version of the same script, and it can be run and put in background (running and not stopped) just by launching it with the & in the end (script.sh &
)
我制作了同一个脚本的 bash 版本,它可以运行并放在后台(运行而不是停止),只需在最后用 & 启动它 ( script.sh &
)
script.sh:
脚本.sh:
#!/bin/bash
while true; do
echo `date`
sleep 30
done
Why the php script is being stopped after launching it in background, and the bash script doesn't? What could cause this different behaviour?
为什么 php 脚本在后台启动后被停止,而 bash 脚本没有?什么可能导致这种不同的行为?
采纳答案by jm666
Usually, the process what do you send to background with the &
and the script waiting for an input from the terminal, going into the stopped state.
通常,进程将您发送到后台的内容&
和脚本等待来自终端的输入,进入停止状态。
E.g. have a bash script valecho
:
例如有一个 bash 脚本valecho
:
#!/bin/sh
read val
echo $val
runnig it as:
将其运行为:
./valecho &
the script will stop.
脚本将停止。
When run it as
当运行它时
echo hello | ./valecho &
will correctly run and finish.
将正确运行并完成。
So, check your php - probably wants some input from the stdin
所以,检查你的 php - 可能需要一些来自 stdin
Edit- based on comment:
编辑- 基于评论:
i'm not an php developer - but just tried the next script (p.php
)
我不是 php 开发人员 - 但只是尝试了下一个脚本 ( p.php
)
<?php
while(true){
? ?echo 'hi '.time()."\n";
? ?sleep(3);
}
?>
with the command:
使用命令:
php -f p.php &
and running nicely... so... sry for confusion...
并且运行良好......所以......请不要混淆......
回答by Lorenzo Marcon
I found what is causing the issue. In PHP, if the readline module is enabled, any command line script will expect an input, even if the script is written to NOT wait for user input.
我找到了导致问题的原因。在 PHP 中,如果启用了 readline 模块,则任何命令行脚本都将期待输入,即使脚本被编写为不等待用户输入。
To check if you have readline support enabled, just run:
要检查您是否启用了 readline 支持,只需运行:
php --info |grep "Readline Support"
and check the output. If you get Readline Support => enabled
then you have readline enabled and you may experience the problem described in the original question.
并检查输出。如果你得到了,Readline Support => enabled
那么你已经启用了 readline,你可能会遇到原始问题中描述的问题。
The proper way to use the cli when is then to explicitly specify that php is not using the terminal for input:
使用 cli when 的正确方法是明确指定 php 不使用终端进行输入:
php myscript.php < /dev/null &
Further info: http://php.net/manual/en/book.readline.php
更多信息:http: //php.net/manual/en/book.readline.php
Alternatives:
备择方案:
./test.php >/dev/null &
or (more creative):
或(更有创意):
nohup php test.php > /dev/null 2>&1 &
(p.s.: I still believe this question belongs to ServerFault, btw.. problem solved!)
(ps:我还是相信这个问题属于ServerFault,顺便说一句..问题解决了!)
回答by Chris Montanaro
From http://php.net/manual/en/book.readline.php:
从http://php.net/manual/en/book.readline.php:
When readline is enabled, php switches the terminal mode to accept line-buffered input. This means that the proper way to use the cli when you pipe to an interactive command is to explicitly specify that php is not using the terminal for input:
当启用 readline 时,php 会切换终端模式以接受行缓冲输入。这意味着在通过管道传输到交互式命令时使用 cli 的正确方法是明确指定 php 不使用终端进行输入:
php myscript.php < /dev/null &