Home  >  Article  >  Operation and Maintenance  >  How to use pid to check whether a process exists in linux

How to use pid to check whether a process exists in linux

WBOY
WBOYOriginal
2022-04-07 12:37:416746browse

In Linux, you can use the kill command to check whether the process exists based on the pid. "kill -0" will not send a signal to the process, but will perform error checking. If the process exists, the returned result is 0. If The process does not exist, the returned result is 1, and the syntax is "kill -0 pid".

How to use pid to check whether a process exists in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to use pid in linux to check whether a process exists

Suppose we want to monitor whether a process is alive and check it every minute. Using the above method, we need to run the ps command and do it once every minute. grep regular search. This overhead seems to be nothing on the server, but what if we want to monitor dozens or hundreds of such processes on the same node at the same time? Therefore, we need to explore some better methods from a performance perspective.

For daemon processes, they usually have their own pid or lock files. We can check whether these files exist to determine whether the process exists. However, under some abnormal circumstances, the process where the pid file exists does not exist. Therefore, you cannot rely on the process's pid file to detect whether the process is alive.

A reliable method is to use "kill -0 pid", kill -0 will not send any signal to the process, but will perform error checking. The command returns 0 if the process exists and 1 if it does not exist.

How to use pid to check whether a process exists in linux

However, this method can only be used for ordinary users to check their own processes, because sending signals to other users' processes will cause an error due to lack of permission and return The value is also 1.

[sw@gentoo ~]$ kill 2993
-bash: kill: (2993) - Operation not permitted
[sw@gentoo ~]$ echo $?
1
[sw@gentoo ~]$

Of course, if you use a privileged user to execute the kill command, there will be no permission problem.

On the other hand, we know that the kernel will export the process information running in the system through the /proc virtual file system. Each process has a /proc/ directory. Therefore, we can convert the detection of the existence of the process into the detection of the existence of the /proc/ directory, which is much simpler.

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of How to use pid to check whether a process exists in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn