根据应用程序的名字删除进程,而不需要根据进程号删除进程。
#!/bin/bash
#ps term
function psterm()
{
[ ${#} -eq 0 ] && echo "usage: $FUNCNAME STRING" && return 0
local pid
pid=$(ps ax | grep "$1" | grep -v grep | awk '{ print $1 }')
echo -e "terminating '$1' / process(es):\n$pid"
kill -SIGTERM $pid
}
#ps grep
ps aux | grep "$1" | grep -v 'grep'
psterm $1
假设该shell文件名为kill.sh,需要删除名为application的进程,输入 kill.sh application就可以删除进程。
具体请参考《Linux 101 Hacks》Page 119-120
附在网上看到的另一个方法:
#! /bin/ksh
ps -e | grep -w $1 | awk '{print $1}' | xargs kill -9
echo "terminating process[$1] successfully"
参考地址: