Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Shell check whether a process exists

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Shell checks whether the process exists. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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 program that belongs to the user, or you can use the user name to specify the process identification number 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, processing only one line at a time. Parameters:

-n reads the next input line and processes the new line with the next command instead of printing the matching line with the first command p flag (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's article on "how to check whether a process exists in Shell" ends here. I hope the above content will be helpful to you so that you can learn more knowledge. if you think the article is good, please 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report