aboutsummaryrefslogtreecommitdiff
path: root/child
blob: 32dab6082ab511cd9c56575f3b165c921c853f3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh

# child -- print pid of youngest grandchild

opt=
for x in "$@"; do
	case "$x" in
	-*) opt="$opt $x"; shift ;;
	*) ;;
	esac
done

[ $# -ne 1 ] && { echo usage: $0 [-?] pid 1>&2; exit 1; }

ps -do pid,etime,comm -k etime $opt |
pid=$1 perl -lane '
	if (/^ *$ENV{pid} / .. 1) {
		next if /^ *$ENV{pid} /;
		last if not /[|`-]/;
		$t = 0; $i = 0;
		$t += $_*(60**$i++) for reverse split /:/, $F[1];
		print "$t $_" if $t > 2;
	}
' |
sort -n |
head -1 |
cut -d\  -f2