Find PID of a Process by Name without Using popen() or system()

会有一股神秘感。 提交于 2019-12-23 10:17:12

问题


I've a process name and I've to send a kill() signal to that process but I need its PID to call kill(). I would only like to use:

popen("pidof process_name");

as the last thing. Is there any other way to find out the process' PID? One way I could think of is to send a socket request to that process and ask to its PID.

The other way is a little too complicated for a simple code I'm writing: to do what pidof command's source code is actually doing (it uses a function call find_pid_by_name() but that's doing a lot of things).

If no simple solution is possible, I've to do this:

system("pkill <process_name>");

and check its return code. But will pkill be available for sure on all Linux machines?


回答1:


You mentioned you were using linux. It isn't the cleanest solution, but you can go through every entry in /proc and check the process name in cmdline against what you are looking for.




回答2:


Use sysctl - Example code

EDIT - it is available in Linux see here




回答3:


Procfs reads are very cheap, many people think of iterating through /proc like they would iterating through / , its really not the case at all.

Save some time by skipping any entry that is under 1000, there's no sense in examining kernel threads. So, basically .. after opendir(), if strtoint() thinks the entry is an int , and that int is greater than or equal to 1000, just read /proc/%d/stat.

Try to avoid the urge to just resolve the 'exe' link, as that is not going to tell you the state of the process that will be receiving the signal. For instance, if the target is in state 'D' (disk sleep), you'd want to know as the signal will not be immediately delivered, or perhaps never, if the D state is perennial.

On most systems, there's going to be 70 - 120 processes to examine and you'll often find your target way before reaching the end.




回答4:


You mention, "One way I could think of is to send a socket request to that process and ask to its PID." It sounds like the process you are trying to kill is a program you wrote.

If that's the case, the cannonical thing to do is to store the pid in a file (usually under /var/run, if you have access to it) while the program is running, and remove the file when the program exits. That way, detecting whether or not the program is running is as simple as

if kill -0 $(cat /var/run/myprog.pid 2>/dev/null) 2>/dev/null; then
    echo Running!
else
    rm -f /var/run/myprog.pid
    echo "Not running."
end

Careful study of all the implications of the above code will probably teach you a lot about how PID files work. Or you can just ask for a more detailed explanation.




回答5:


This seems to work fine for me.

You might want to give the full path of the process though, lest you kill a process that has a similar name.

The main advantage of this is that you can specify the signal that you want to send.

system("killall -s 9 process_name");



回答6:


why not to use fcntl with F_GETOWN ?




回答7:


To complete @GregRogers answers, you just need to check implementation of pidof

  • https://gitlab.com/procps-ng/procps/blob/master/pidof.c#L147
  • https://gitlab.com/procps-ng/procps/blob/master/proc/readproc.h#L288
  • https://gitlab.com/procps-ng/procps/blob/master/proc/readproc.c#L932

as you can see it only searches on /proc directory.



来源:https://stackoverflow.com/questions/374997/find-pid-of-a-process-by-name-without-using-popen-or-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!