killall <process name>
但若是有多個 prefix 相同的程式就要分別指定
killall test_a test_b test_c
為了方便使用,初步寫一版可使用部分執行檔來終止程式的腳本,下例腳本是使用以下 /bin/sh 測試:
- busybox v1.31
- dash 0.5.8-2.10
Script example
pname="$1"
if [ "x$pname" = x ]; then
echo "syntax: <partial process name>"; exit 1 ; fi
ps -a | grep "$pname" | \
grep -v grep | \
sed -e "s/^[ ]*\([0-9][0-9]*\) .*$/\1/g" | \
while read pid ; do kill $pid ; echo "terminate $pid" ; done
Test output
~$ sleep 86400 &
[1] 2824
~$ sleep 86402 &
[2] 2832
sh 加上 -x 可除錯執行
~$ sh -x terminate_pname.sh sleep
+ pname=sleep
+ [ xsleep = x ]
+ ps -a
+ grep sleep
+ grep -v grep
+ sed -e s/^[ ]*\([0-9][0-9]*\) .*$/\1/g
+ read pid
+ kill 2824
+ echo terminate 2824
terminate 2824
+ read pid
+ kill 2832
+ echo terminate 2832
terminate 2832
+ read pid
[1]- Terminated sleep 86400
[2]+ Terminated sleep 86402
沒有留言:
張貼留言