bash find and kill processes

A shell script wants your job

Today while I was working I saw that one of the machines was lying very badly. I go into it watching a cron bump into a hell of a lot of zombie processes (roughly around 50-60). There was no way I could kill them all with killall so I had to make a little more competent solution to the problem – to draw an elementary bash a script to find and kill processes. 50-PIDs are not easy to write by hand :D. I scratched the script for a minute and it is very simple, but it still deserves attention 🙂

At its base is the conveyor

ps ax | grep -v grep | grep process_name | awk '{print $1}')

Here we get a sheet with all the PIDs of the process that we have to kill by excluding grep from this list. Now that we have the list, things become easy, everything turns into one for. Here is the end result

#!/bin/bash

PR=$(ps ax | grep -v grep | grep process_name | awk '{print $1}')

for PID in $PR
do
echo "$PID will be killed"
kill -9 $PID
done

Can be “tuning” as the name is taken as an argument after the name of the script and thus is called as executable binary. However, it is not a very good practice to have such frequent cases 😀 But it never prevents us from being protected from any

Enhanced by Zemanta

Leave a Reply

Your email address will not be published. Required fields are marked *

Anti SPAM *