在后台运行命令行脚本进入处于停止状态 [英] Command line script run in background goes in stopped state
问题描述
我有一个简短的PHP程序脚本,我从CLI简单地运行它
I have a short php utility script, I run it from cli simply with:
php myscript.php
该脚本将一直工作,定期执行某些任务(这个问题不相关)。它不需要来自用户的任何输入。
它运行后,我通常preSS <大骨节病> CTRL 骨节病> + <大骨节病>以Z 骨节病>然后运行 BG
将在后台进程和一切都很好。
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+z and then run bg
to put the process in background, and everything is fine.
如果我运行它为:
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
即使在运行 BG
在这一点上没有帮助,我要运行 FG
,然后<大骨节病>再次CTRL 骨节病> + <大骨节病>以Z 骨节病>和 BG
来使其工作。
even running bg
at this point doesn't help, I have to run fg
, then CTRL+z and bg
again to make it work.
这是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?
更新:
我犯了同样的脚本bash的一个版本,它可以运行,并把在后台(运行和没有停止)刚刚通过与&放大器启动它;到底( script.sh&安培;
)
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 &
)
script.sh:
script.sh:
#!/bin/bash
while true; do
echo `date`
sleep 30
done
为什么PHP脚本正在后台启动后停止,bash脚本不?
这是什么原因不同的行为?
Why the php script is being stopped after launching it in background, and the bash script doesn't? What could cause this different behaviour?
推荐答案
通常情况下,你发送到后台与&放东西的过程;
和脚本等待从终端输入时,进入停止状态。
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.
例如。有一个bash脚本 valecho
:
E.g. have a bash script valecho
:
#!/bin/sh
read val
echo $val
运行过程中出现它作为:
runnig it as:
./valecho &
该脚本将停止。
在运行它
echo hello | ./valecho &
将正常运行和完成。
will correctly run and finish.
所以,请检查你的php - 可能想从一些输入标准输入
So, check your php - probably wants some input from the stdin
修改 - 基于评论:
我不是一个PHP开发 - 但只是尝试下一个脚本( p.php
)
i'm not an php developer - but just tried the next script (p.php
)
<?php
while(true){
echo 'hi '.time()."\n";
sleep(3);
}
?>
使用以下命令:
php -f p.php &
和运行的很好...所以... SRY混乱......
and running nicely... so... sry for confusion...
这篇关于在后台运行命令行脚本进入处于停止状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!