In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
I.
Pgrep is a tool to query the process by the name of the program, which is generally used to determine whether the program is running. In server configuration and management, this tool is often used, simple and straightforward.
Usage:
# pgrep [option] [Program name]
Pgrep [- flvx] [- n |-o] [- d delim] [- P ppidlist] [- g pgrplist] [- s sidlist] [- u euidlist] [- U uidlist] [- G gidlist] [- J projidlist] [- t termlist] [- T taskidlist] [- c ctidlist] [- z zoneidlist] [pattern]
Common parameters
-l list the program name and process ID
-o ID at the beginning of the process
-n ID of process termination
In addition, you can use another ps command: (click to view the ps command for details)
Ps x | grep xxx | awk'{print $1}'
Example:
Ps x | grep java | awk'{print $1}'
Note:
1. Xxx is the name of the command executed
2. For example, get the java process [pid] under the current user.
[admin@vm168a ~] $ps x | grep java | awk?'{print $1}'
16920
3. Three commands are used, ps, grep and awk.
If you can't get it this way, you can use the ps command:
Ps-ef | grep xxx | grep-v 'grep' | awk' {print $2}'
[yanue@server ~] $ps-ef | grep nginx | grep-v 'grep' | awk' {print $2}'
II.
Interactive Bash Shell acquisition process pid
On the premise that the process name (name) is known, there are many ways for interactive Shell to obtain process pid. The typical way to obtain pid through grep is (- v grep is added here to avoid matching to grep processes):
Ps-ef | grep "name" | grep-v grep | awk'{print $2}'
Or do not use grep (where the initial [] is added to avoid matching to the process of awk itself):
Ps-ef | awk'/ [n] ame/ {print $2}'
If only the x parameter is used, pid should be in the first place:
Ps x | awk'/ [n] ame/ {print $1}'
The easiest way is to use pgrep:
Pgrep-f name
If you need to kill the process after finding pid, you can also use pkill:
Pkill-f name
If it is an executable program, you can use pidof directly
Pidof name
Bash Shell script gets process pid gets process pid based on process name
When using the Shell script to get the process pid, if you use the above command directly, multiple pid results will appear, such as:
12345ps x | grep | grep-v grep | awk
There are several results when you execute process-monitor.sh:
$> sh process-monitor.sh4036 3098 3099
Further inspection reveals that the extra processes are actually (temporary) processes of the child Shell:
Root 3036 2905 0 09:03 pts/1 00:00:45 / usr/java/jdk1.7.0_71/bin/java... nameroot 4522 2905 0 16:12 pts/1 00:00:00 sh process-monitor.sh nameroot 4523 4522 0 16:12 pts/1 00:00:00 sh process-monitor.sh name
Where 3036 is the process pid that needs to be found, and 4522 and 4523 are the pid of the child Shell. To avoid this, you need to further clarify the search criteria, and considering that you are looking for a Java program, you can match through the keyword of Java:
12345ps-ef | grep | grep | grep-v grep | awk gets the Shell script's own process pid
Here are two instructions: 1. $$: the current Shell process's pid 2. $!: the pid of the previous background process can use these two instructions to get the corresponding process pid. For example, if you need to get the pid of an executing process (and write to the specified file):
MyCommand & & piddance updated myCommand & echo $! > / path/to/pid.file
Notice that you execute $in the script! Only the background process pid of the child Shell is displayed, and if the child Shell has not previously started the background process, there is no output.
Check whether the specified process exists
After getting the pid, you can also check whether the corresponding process exists (running) according to the pid. This method can also be used for the process specified by the kill.
If ps-p $PID > / dev/nullthen echo "$PID is running" # Do something knowing the pid exists, i.e. The process with $PID is running fi
Third, judge the status of the process
# PID= `ps-ef | grep java | grep flume | awk'{print $2}'`
# ps-ef | grep hello | awk'{print $2}'| xargs kill-9
#! / bin/bash
Condir=/app/cfg/content-hist-b
Startfile=/app/etc/init.d/content-hist-b
Pid=content-hist-b
Echo "D&Gby1900d129" | sudo-S / usr/sbin/sysctl-w vm.drop_caches=3
Count= `pgrep-f $Pid` & & echo $count
If [- n "$count"]; then
Sleep 1
Echo "Prcoess is busy"
Kill-9$ count
Sleep 60
Cd $Condir & & sh clean.sh & & $Startfile restart
Else
Echo "Prcoess is stop"
Cd $Condir & & sh clean.sh & & $Startfile restart
Fi
Exit
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.