Linux, get process ID (PID)

The following command will allow shell script to grab PID for further processing:

ps -ef | grep KEYWORD | grep -v grep | awk '{print $2}'

For example, you would like to grep the process ID of nagios below:

[user@server ~]$ ps aux | grep nagios
nagios 18020 0.0 0.0 17296 2840 ? Ss 06:21 0:00 /bin/nagios -d /etc/nagio.cfg
nagios 18025 0.0 0.0 10796 2308 ? S 06:21 0:00 /bin/nagios --worker /var/rw/nagios.qh
nagios 18026 0.0 0.0 10796 2340 ? S 06:21 0:00 /bin/nagios --worker /var/rw/nagios.qh
nagios 18027 0.0 0.0 10796 2408 ? S 06:21 0:00 /bin/nagios --worker /var/rw/nagios.qh
nagios 18028 0.0 0.0 10796 2288 ? S 06:21 0:00 /bin/nagios --worker /var/rw/nagios.qh

ps -ef | grep 'nagios -d' | grep -v grep | awk '{print $2}'

will gives:
18020

Be the first to comment

Leave a Reply

Your email address will not be published.


*