In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
How does linux check whether a process exists? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
This question seems to be very simple, "ps-ef | grep xx" on it! Of course, this is fine, but if we pay attention to performance, I'm afraid it's not a good idea.
Suppose we now want to monitor the survival of a process, checking it every minute, using the above method to run the ps command every minute and do a grep regular lookup. This overhead may not seem like much 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, it is necessary for us to explore some better ways from the perspective of performance.
For daemon processes, they usually have their own pid or lock files, and we can check the existence of these files to determine whether the process exists. However, in some abnormal cases, the pid file existence process does not exist. Therefore, you cannot rely on the process's pid file to detect whether the process is alive or not.
One reliable way is to use "kill-0 pid", which does not send any signals to the process, but does error checking. The command returns 0 if the process exists and 1 if it does not exist.
[sw@gentoo ~] $ps PID TTY TIME CMDpts/0 00:00:00 bashpts/0 00:00:00 ps [sw@gentoo ~] $kill-0 15091 [sw@gentoo ~] $echo $? [sw@gentoo ~] $kill-0 15092-bash: kill: (15092)-No such process [sw@gentoo ~] $echo $? [sw@gentoo ~] $
However, this method can only be used for ordinary users to check their own processes, because sending signals to other users' processes will make an error because they do not have permissions, and the return value is 1.
[sw@gentoo] $kill 2993-bash: kill: (2993)-Operation not permitted [sw@gentoo ~] $echo $? 1 [sw@gentoo ~] $
Of course, if you use privileged users to execute kill commands, there will be no permission issues.
This is the answer to the question about how to check whether the process exists in linux. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.