I am trying to develop a script will find the latest process that created by certain pts, for example pts/0. Then I need to print PID of this process.
ps -ft $name
???
``
CodePudding user response:
ps
offers to sort the returned list of processes by a field of your choice. In this case you'll sort by etime
(execution time)..
Like this:
name="pts/2" # ... for example
ps -t "${name}" -o pid --sort etime --no-header
25624
7856
To get only the most recent one, pipe to head
:
ps -t "${name}" -o pid --sort etime --no-header | head -n1
25642
CodePudding user response:
Check out below script to get latest processes PID by pts
ps -ef | sort -k5r | grep pts | awk '{print $6}' | sort -u | grep -v "?" | while read pts; do
echo "pts id $pts"
ps -ft $pts
echo "==================="
done