Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting the output of ps command,getting process id and killing that process using shell script

Tags:

shell

I want to write a shell script to find the running process for a given user and kill the process by getting the respective process ID.

Its like

ps -ef | grep dinesh

After this, i am getting the output as the following

dinesh 19985 19890  0 11:35 pts/552  00:00:00 grep dinesh

Here 19985 is the process ID. I want to kill that process.

How can i achieve this using script?

I have to parse the ps command output and get the process ID

Thanks in advance.

like image 873
Dinesh Avatar asked May 30 '12 06:05

Dinesh


2 Answers

kill `ps -ef | grep dinesh | awk '{ print $2 }'`
like image 91
nab Avatar answered Oct 13 '22 14:10

nab


What if there is more than one process defined by the string 'dinesh'? What about the grep process itself? This is a more complete answer

ps -ef | grep dinesh | grep -v grep | awk '{print $2}' | xargs kill -9

like image 27
Woodwose Avatar answered Oct 13 '22 14:10

Woodwose