3

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.

fionn
  • 57
  • 11
Karun
  • 447
  • 5
  • 17

6 Answers6

3

you can use pidof

kill -9 `pidof <your application name>`

your application name could be /usr/sbin/watchdog

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
2

Maybe this is an ideal world solution...

To get the watchdog PID, you only need to open the file '/var/run/watchdog.pid'.

Once you know the PID of a process, the best way to kill the process is not to call the 'system' function, but to call the 'kill' function of the UNIX API:

#include <sys/types.h>
#include <signal.h>

int
kill(pid_t pid, int sig);

This avoids spawning new processes.

If the file '/var/run/watchdog.pid' does not exist, you can revert to system/kill/pidof.

jfg956
  • 16,077
  • 4
  • 26
  • 34
1

To display all processes owned by the current user type ps ux and hit return:

$ ps ux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
jhaas 3064 0.1 3.6 18324 9088 ? S 17:55 0:00 /usr/bin/gnome-session
jhaas 3107 0.0 0.3 3128 968 ? S 17:55 0:00 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
....

Now, if you want to terminate for example the emacs process you would look up the process identifier (PID) in the above table (3216), and say:

$ kill -9 3216

Source: http://linux.about.com/library/cmd/blcmdl_kill.htm

Oben Sonne
  • 9,893
  • 2
  • 40
  • 61
dknaack
  • 60,192
  • 27
  • 155
  • 202
1

You should iterate over the subdirectories in /proc, finding the processes you want to kill. Then use kill(2).

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
1

Take a look on: http://linux.die.net/man/5/proc

You can search /proc file system and find a link /proc/$(PIC)/exe that points to /usr/sbin/watchdog

Once you get pid just kill it (see man 2 kill)

Artyom
  • 31,019
  • 21
  • 127
  • 215
  • proc file system lists the process Ids, how can I find the process Id of specific application(say watchdog) from there. – Karun Jul 19 '11 at 09:38
  • @Karun: use pidof , you can checkout my answer – TheOneTeam Jul 19 '11 at 09:59
  • @Karun: for each process there is a directory named after the PID. In this directory you'll find the information you're looking for (as described in the linked man-page). – Oben Sonne Jul 19 '11 at 10:02
0

pgrep is actually quite nice for this, especially if you ever intend to use a regular expression to match against process names. It is part of the procps package, which should already be installed on any linux system (ps is in the same package). Here's an example usage:

psychotic@bismuth ~ $ pgrep 'fire.*'
2902
2903
2904
6979
psychotic@bismuth ~ $ pgrep -l 'fire.*'
2902 firegl
2903 firegl
2904 firegl
6979 firefox-bin
psYchotic
  • 301
  • 1
  • 4