Redirecting the output of the ps command, getting the process id and killing the process with a shell script

I want to write a shell script to find the executable process for a given user and kill the process by getting the corresponding process id.

Its like

ps -ef | grep dinesh

After that, I get output like the following

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

Here 19985 is the process identifier. I want to kill this process.

How can I achieve this using a script?

I need to parse ps command output and get process id

Thanks in advance.

+5
source share
2 answers
kill `ps -ef | grep dinesh | awk '{ print $2 }'`
+11
source

What if there are several processes defined by a string 'dinesh'? How about the grep process itself? This is a more complete answer.

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

+4
source

All Articles