Oscar Godson
Oscar Godson

Reputation: 32776

ps aux auto close app

I'm trying to setup a task to kill certain server processes when the server gets into a weird state such as when it fails to boot one process, but another process gets keeps running and so not everything boots up. This is mainly a task for development so you can do jake killall to kill all processes associated with this project.

I'm having trouble figuring out how to get the pid after doing: ps aux | grep [p]rocess\ name | {HOW DO I GET THE PID NOW?} and then after getting the ID how do I pass that to kill -9 {PID HERE}

Upvotes: 0

Views: 177

Answers (2)

hashier
hashier

Reputation: 4750

You could also you killall <program> or pkill <program> or pgrep <program>

Upvotes: 2

fedorqui
fedorqui

Reputation: 290255

The PID is the second column, so you can do

ps aux | grep [p]rocess\ name | awk '{print $2}'

All together,

my_pid=$(ps aux | grep [p]rocess\ name | awk '{print $2}')
kill -9 $my_pid

Upvotes: 2

Related Questions