Shell get process PID

    There are some differences between the Linux interactive shell and the shell script, mainly because the latter has an independent running process, so the two are also different in obtaining the process pid.

Interactive Bash Shell to get process pid

#On the premise that the process name (name) is known, there are many ways for the interactive shell to obtain the process pid. The typical way to obtain the pid through grep is (-v grep is added here to avoid matching the grep process):
ps -ef | grep "name" | grep -v grep | awk '{print $2}'
#Or do not use grep (the purpose of adding [] to the first letter of the name is to avoid matching the process of awk itself):
ps -ef | awk '/[n] ame/{print $ 2}'
#If only the x parameter is used, the pid should be first:
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 the pid, you can also use pkill:
pkill -f name
#If it is an executable program, you can use pidof directly
pidof name

learn shell addresses

https://www.yiibai.com/shell/unix-special-variables.html

http://wiki.jikexueyuan.com/project/shell-learning/gorgeous-printf-output.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324929179&siteId=291194637