In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you linux's way to see if a process exists. The editor thought it was very practical, so I shared it with you to learn. The following information is about linux to see if there is a method for the process.
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 $? [sw@gentoo ~] $
Of course, if you use privileged users to execute kill commands, there will be no permission issues.
This is the end of the method for linux to see whether the process exists. Solving the problem is not just the way analyzed in the article and everyone, but the accuracy of the method analyzed in this article is beyond doubt. If you like this article, you might as well share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.