Search code examples
linuxgrepkillps

Kill a process from Linux Application


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.


Solution

  • you can use pidof

    kill -9 `pidof <your application name>`
    

    your application name could be /usr/sbin/watchdog