我有这个:
while [ "ps aux | grep '[0]:00 Xvfb :99 -screen 0 1024x768x16'" ]
do
echo "sleep"
ps aux | grep "[0]:00 Xvfb :99 -screen 0 1024x768x16"
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
这给了我:
sleep
sleep
sleep
sleep
sleep
sleep
root 7 0.5 1.5 207336 31620 ? Sl 09:31 0:00 Xvfb :99 -screen 0 1024x768x16
sleep
root 7 0.4 1.5 207336 31620 ? Sl 09:31 0:00 Xvfb :99 -screen 0 1024x768x16
sleep
root 7 0.3 1.5 207336 31620 ? Sl 09:31 0:00 Xvfb :99 -screen 0 1024x768x16
...
...
...
Run Code Online (Sandbox Code Playgroud)
如何更改我的oneliner,以便一旦ps aux | grep返回0(表示Xvfb进程正在运行)退出循环?
目前,您正在评估字符串是否为非空(在bash中,[ "string" ]相当于[ -n "string" ]).
如果要在模式匹配时退出,请使用以下命令:
while ! ps aux | grep -q '[0]:00 Xvfb :99 -screen 0 1024x768x16'; do
echo "sleep"
sleep 1
done
Run Code Online (Sandbox Code Playgroud)
-qgrep 的选项启用"安静模式",因此输出被抑制.
请注意,您可以(应该?)使用pgrep而不是管道:
while ! pgrep '0:00 Xvfb :99 -screen 0 1024x768x16'; do
# ...
Run Code Online (Sandbox Code Playgroud)