简体   繁体   中英

Get pid of last started instance of a certain process

I have several instances of a certain process running and I want to determine the process id of the one that has been started last. So far I came to this code:

ps -aef | grep myProcess | grep -v grep | awk -F" " '{print $2}' |
while read line; do
  echo $line
done

This gets me all process ids of myProcess. Somehow I need to compare now the running times of this pids and find out the one with the smallest running time. But I don't know how to do that...

An easier way would be to use pgrep with its -n, --newest switch.

Select only the newest (most recently started) of the matching processes.

Alternatively, if you don't want to use pgrep , you can use ps and sort by start time:

ps -ef kbsdstart

Use pgrep . It has a -n (newest) option for that. So just try

pgrep -n myProcess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM