Friday, December 17, 2010

How to kill a process by name in Bash script

Following code kills Safari application:

ps -axcopid,command | grep "Safari" | awk '{ system("kill -9 "$1) }'

Following pkill.sh script will kill all processes matching the given name (case-insensitive):

#!/bin/sh

if [[ $1 -eq "" ]]; then
    echo "Usage: pkill.sh <process name>"
    exit
fi

for id in `ps -axcopid,command | grep -i -e $1 | awk '{ print $1 }'`; do
  kill -9 $id
done

No comments:

Post a Comment