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.
kill `ps -ef | grep dinesh | awk '{ print $2 }'`
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With