Loading...

Retrieve process id from ps

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

Example process without the grep command

tan@omega:~/bin> ps -Af | grep '[f]ile-watchdog.sh start'
tan 40502 1 0 12:23 pts/1 00:00:00 bash ./file-watchdog.sh start
tan 40504 40502 0 12:23 pts/1 00:00:00 bash ./file-watchdog.sh start

Show only process id

tan@omega:~/bin> ps -Af | grep '[f]ile-watchdog.sh start' | awk '{ print $2}'
40502
40504

Show first process

tan@omega:~/bin> ps -Af | grep '[f]ile-watchdog.sh start' | awk '{ print $2}' | head -n 1
40502

Show last process

tan@omega:~/bin> ps -Af | grep '[f]ile-watchdog.sh start' | awk '{ print $2}' | tail -n 1
40504
Please remember the terms for blog comments.