There are some differences between the interactive shell and shell scripts of Linux, mainly because of the existence of a separate running process, so there is a difference between the acquisition process PID.Interactive Bash Shell get process pid
With the known process name (name), the interactive Shell gets the process PID in a number of ways, typically by using grep to get the PID (where the-v grep is added to avoid matching to the grep process):
ps -ef | grep "name" | grep -v grep | awk ‘{print $2}‘
Or do not use grep (where the initials are added [] to avoid matching the process to awk itself):
ps -ef | awk ‘/[n]ame/{print $2}‘
If you use only the x parameter, the PID should be in the first place:
ps x | awk ‘/[n]ame/{print $1}‘
The simplest way is to use pgrep:
pgrep -f name
If you need to find the PID and kill the process, you can also use Pkill:
pkill -f name
If the program is executable, you can use pidof directly.
pidof name
Get Jmeter_pid
[[email protected] ~]# ps -ef | grep "jmeter" | grep -v grep | awk ‘{print $2}‘ 184061840818442
Writing the Shell
#!/bin/bash#jmeter kill and startecho -e ‘\033[32m--------Jmeter---------------\033[0m‘echo "kill掉jmeter-server 进程................... \n"kill -9 `ps aux | grep jmeter | grep -v grep | awk ‘{print $2}‘`sleep 3cd /root/tools/apache-jmeter-3.3/bin/echo "启动jmeter-server"nohup ./jmeter-server &echo -e ‘\033[32m------------欢迎使用-----------------\033[0m‘
Linux acquires JMeter PID and kill with Boot