Dinesh
Dinesh

Reputation: 16428

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

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.

Upvotes: 6

Views: 7077

Answers (2)

Woodwose
Woodwose

Reputation: 61

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

Upvotes: 6

nab
nab

Reputation: 4861

kill `ps -ef | grep dinesh | awk '{ print $2 }'`

Upvotes: 18

Related Questions