Kill a process from Linux Application

Karun picture Karun · Jul 19, 2011 · Viewed 7.4k times · Source

What is best and most efficient way to find pid of a specific task. Say:

ps -ef | grep "\/usr\/sbin\/watchdog" | cut -d" " -f2

Is there any more efficient way to find the same. I want to kill the watchdog process from my application. I am thinking using system command to do the same.

system("kill -9 `ps -ef | grep "\/usr\/sbin\/watchdog" | cut -d" " -f2`);

Is there any more optimized way of doing the same.

Answer

Kit Ho picture Kit Ho · Jul 19, 2011

you can use pidof

kill -9 `pidof <your application name>`

your application name could be /usr/sbin/watchdog