我有這個 bash 腳本:
#!/bin/bash
if [ "$#" -eq 4 ]
then
rep_origine="$1"
rep_dest="$2"
temps_exec="$3"
temps_refr="$4"
else
echo "Usage : $0 [files directory] [destination directory] [execution time$
exit 2
fi
cp -R "$rep_origine" "$rep_dest" &
cp_process="$!"
while [ "$cp_process" -eq "$!" ]
do
cp_process="$!"
sleep "$temps_exec"; kill -STOP "$cp_process"
sleep "$temps_refr"; kill -CONT "$cp_process"
done
我希望我的回圈在 cp 命令結束時結束。因此,我提出當最后一個 PID 與 cp 的 PID 不同時,回圈應該結束但它不起作用。
我看不到如何指示回圈應該在 cp 命令結束時結束。
uj5u.com熱心網友回復:
我將使用kill非有害信號0來檢查行程是否仍然存在:
while kill -0 $cp_process 2>/dev/null
do
# work indicator:
echo -n '.'
sleep 1
done
如果目的是暫時停止回圈中的行程:
while kill -STOP $cp_process 2>/dev/null
do
# do work while the process is stopped here
kill -CONT $cp_process
# give the process execution time:
sleep $temps_exec
done
uj5u.com熱心網友回復:
沒有要求,但使用訊息中的括號表示可選引數,同時您嚴格檢查 4 個引數。
Linux 有一個超時程式,用于停止一個在給定時間內沒有完成的行程——也許這適合你。
#!/bin/bash
if [ "$#" -eq 4 ]
then
rep_origine="$1"
rep_dest="$2"
temps_exec="$3"
temps_refr="$4"
else
echo "Usage : $0 source_dir destination_dir execution_time temps_refr
exit 2
fi
timeout temps_exec cp -R "$rep_origine" "$rep_dest"
有關詳細資訊,請參閱man timeout。
uj5u.com熱心網友回復:
在這種情況下,您可以簡單地使用 ps 和 grep 來檢查行程是否正在運行,
前任:
#!/bin/bash
if [ "$#" -eq 4 ]
then
rep_origine="$1"
rep_dest="$2"
temps_exec="$3"
temps_refr="$4"
else
echo "Usage : $0 [files directory] [destination directory] [execution time$ "
exit 2
fi
cp -R "$rep_origine" "$rep_dest" &
cp_process="$!"
# check if the process is still running or not
while true; do
check=$(ps -ef | grep -w $cp_process | grep -v grep | wc -l)
if [ $check -ge "1" ]; then
echo "process $cp_process is still running"
else
echo "Process $cp_process has been completed"
break
fi
sleep 1
done
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468638.html
