Why does the 'pid' keep changing every time?

HI

I checked the pid of a process on my machine using this command.

ps -ef | awk '/process_name/{ print $2 }'

I get the result as 12245
I check it again after 2 mins, I get the result as 12264
I check it again after 2 mins, I get the result as 12289
It keeps on this. How does this pid work?

And, I also have another question. By using the pid, Can I know which process it belongs to?

ps -ef | grep pid#

I suspect that if you temporarily replace $2 by $0 you will find that the awk statement is in fact selecting its own pid.
Try putting square brackets around one of the characters of the process name you are looking for, e.g.:

ps -ef | awk '/[l]s/{ print $2 }'

Belt and braces approach in case there is more than one hit.

ps -ef|grep 'process_name'|grep -v "grep"|awk '{print $2}'|while read PID
do
      echo "PID=${PID}"
done

Ps. Scrutinizer is right, you were getting PID of the awk.

The answer to your supplementary is:

ps -fp${PID}

The process id (PID) is an integer assigned by the operating system at process creation time that uniquely identifies that process. On most operating systems, it's simply an incrementing counter; on OpenBSD, PID generation is randomized. Two processes cannot share a PID, but PID values may be reused. The PID does not change during the lifetime of a process. A different PID is a different process.

If you know the PID, you can use ps to find the process info.

ps -p PID