-1

执行此 bash 脚本时出现错误,该脚本会打印“hello”,直到后台进程继续执行。

ps command not found 

我以前做过几次,但不知道为什么这次我会出错。

./a.sh &
while ps -p $! > /dev/null; do
       echo hello
done
4

2 回答 2

1

这并不能解决您明显PATH的问题,但是对于您想要做的事情,有一种比ps重复调用更简单的方法。

# Start your script in the background, remembering its process ID
./a.sh & A_PID=$!

# Start another background job that echos hello (once per second, to
# avoid a flood of hellos). Remember its process ID as well
( while : ; do echo hello; sleep 1 done ) & LOOP_PID=$!

# Now wait for a.sh to finish...
wait $A_PID

# ... and kill the hello job
kill $LOOP_PID
于 2013-10-25T18:12:33.230 回答
0

调试这个问题我建议以下

 nohup ./a.sh &
 p1=$!

 while ps -p $p1 
 do
    echo hello
    sleep 1
 done
于 2013-10-25T17:41:47.307 回答