The following bash shell script looks at the output of “ps” command to identify the PID of a process. This script takes name of the process as command line argument.

[neo@techpulp ~]# cat getpid.sh
#!/bin/bash

ps -e | grep "$1$" | awk '{print $1;}'
[neo@techpulp ~]#

The first part “ps -e” displays the list of all processes in the system. The “grep” command looks for a line ending with the process name supplied as first command line argument to the script. The “awk” command prints the first word in the matching line.

You will see one PID per line if there are multiple processes with the same name. For example, Apache web server runs 10 server processes with name “httpd“.

[neo@techpulp ~]# ./getpid.sh httpd
4201
4205
4206
4207
4208
4209
4210
4211
4212
[neo@techpulp ~]#