Requirement 1: Kill all the running processes owned by a particular user.
# pgrep -u nagios
Requirement 2: Kill all processes that are running with some process-name or string (from ps output)
Using "For" loop:
Using "Xargs":
[root@hostxyz ~]# ps aux | grep -v grep | grep [username/some string] | awk '{print $2}' | xargs kill -9
Sample execution:
List all the Process IDs of user
# pgrep -u <username>
Find Real UID of user
# id <username>
# id <username>
Kill all the processes owned by user with Real UID specified.
# pkill -U <Real UID>
# pkill -U <Real UID>
Example:
Killing all the processes owned by userid 'nagios'
# pgrep -u nagios
# id nagios
uid=510(nagios) gid=510(nagios) groups=510(nagios)
# pkill –U 510
Requirement 2: Kill all processes that are running with some process-name or string (from ps output)
Using "For" loop:
[root@hostxyz ~]# for i in `ps -ef | grep -v grep | grep [username/some string] | awk '{print $2}'`; do kill -9 $i; echo “process id $i is killed”; done
Using "Xargs":
[root@hostxyz ~]# ps aux | grep -v grep | grep [username/some string] | awk '{print $2}' | xargs kill -9
Sample execution:
To kill all processes that has string "jboss" in it
[root@hostxyz ~]# for i in `ps -ef | grep -v grep | grep jboss | awk '{print $2}'`; do kill -9 $i; echo "Process id $i is killed"; done
[root@hostxyz ~]# ps aux | grep -v grep | grep jboss | awk '{print $2} | xargs kill -9
[root@hostxyz ~]# ps aux | grep -v grep | grep jboss | awk '{print $2} | xargs kill -9