In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use Shell to check whether a process exists". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Shell to check whether a process exists.
In the operation and maintenance work, in order to ensure the normal operation of the business, we need to check regularly whether the specified process still exists. Once the process exits, it indicates that there may be a problem with the service, and the operation and maintenance engineer can deal with the problem as soon as possible. Today I'd like to share with you a practical shell script-checking your process status all the time.
When monitoring a process, we generally need to get the ID of the process. The process ID is the unique identity of the process, but sometimes there may be multiple processes with the same process name running under different users on the server.
The following function GetPID gives the process ID function to obtain the specified process name under the specified user (currently only one process started under this user is considered). It has two parameters: the user name and the process name. It first uses ps to find the process information, while filtering out the required processes through grep, and finally finds the ID value of the required process through sed and awk (this function can be modified according to the actual situation. For example, other information needs to be filtered.
Monitor the process
Function GetPID # User # Name {PsUser=$1 PsName=$2 pid= `ps-u $PsUser | grep $PsName | grep-v grep | grep-v vi | grep-v dbx\ n | grep-v tail | grep-v start | grep-v stop | sed-n 1p | awk'{print $1} '`echo $pid}
The example demonstrates:
1. Source program (for example, find a process ID with user root and process name TestApp)
PID= `Getpid root TestApp` echo $PID
two。 Result output
11426 [dyu@xilinuxbldsrv shell] $
3. Result analysis
As can be seen from the output above: 11426 is the process ID of the TestApp program under the root user.
4. Detailed explanation of command
(1) ps: view the instant process information in the system. Parameters:
-u
< 用户识别码 >Lists the status of the programs that belong to the user, or you can use the user name to specify
-p
< 进程识别码 >Specify the process identification code and list the status of the process
-o specify the output format
(2) grep: used to find the current line in the file that matches the string. Parameters:
-v reverse selection, that is, the line that shows no 'search string' content
(3) sed: a non-interactive text editor that edits files or standard input-exported files and processes only one line at a time. Parameters:
-n read the next input line and process the new line with the next command instead of the first command
P flag prints matching lines
(4) awk: a programming language for processing text and data under linux/unix. In addition to being used on the command line, it is used more as a script. Parameters:
-F fs or-field-separator fs: specifies the input file break delimiter, where fs is a string or a regular expression, such as-F:.
PS: sometimes it is possible that the process does not start. The following function is to check whether the process ID exists, if the process is not running output:
The process does not exist. # check whether a process exists if ["- $PID" = "-"] then {echo "The process does not exist."} fi. I believe you have a better understanding of "how to use Shell to check whether a process exists". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.