执行此 bash 脚本时出现错误,该脚本会打印“hello”,直到后台进程继续执行。
ps command not found
我以前做过几次,但不知道为什么这次我会出错。
./a.sh &
while ps -p $! > /dev/null; do
echo hello
done
这并不能解决您明显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
调试这个问题我建议以下
nohup ./a.sh &
p1=$!
while ps -p $p1
do
echo hello
sleep 1
done