Centos8 environment pidof to obtain accurate process pid

The basic idea:
get all the pids of the process, and then get the process name according to the status file in the process file system corresponding to the current pid, and the pid of the current process is the exact match with the current process name.

app_name="test"
real_pid=-1
for pid in `pidof $app_name`; do
	process_name = `cat /proc/${
     
     pid}/status/|grep "Name"|awk '{print $2}'`
	if [ $process_name == $app_name ]; then
		real_pid=${pid}
	fi
done
echo ${real_pid}

Guess you like

Origin blog.csdn.net/sun172270102/article/details/108071147