rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | I_WANT_A_BROKEN_PS=yes |
| 4 | export I_WANT_A_BROKEN_PS |
| 5 | all=no |
| 6 | if [ "x$1" = "x-a" ] |
| 7 | then |
| 8 | all=yes |
| 9 | fi |
| 10 | export all |
| 11 | |
| 12 | cat >/tmp/awk.xxx$$ <<'!' |
| 13 | BEGIN{ |
| 14 | state["D"] = "Spinwait"; |
| 15 | state["I"] = "Idle"; |
| 16 | state["J"] = "Jail"; |
| 17 | state["R"] = "Ready"; |
rsc | cb27443 | 2004-03-25 23:03:22 +0000 | [diff] [blame] | 18 | state["O"] = "Running"; |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 19 | state["S"] = "Sleep"; |
| 20 | state["T"] = "Stopped"; |
| 21 | state["Z"] = "Zombie"; |
| 22 | state["W"] = "Fault"; |
| 23 | state["X"] = "Moribund"; |
| 24 | } |
| 25 | |
rsc | c57a737 | 2004-04-19 05:51:59 +0000 | [diff] [blame] | 26 | function statestr(s, wchan) |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 27 | { |
rsc | 3351416 | 2005-01-04 21:14:58 +0000 | [diff] [blame] | 28 | if(wchan != s && wchan != "?" && wchan != "-") |
rsc | c57a737 | 2004-04-19 05:51:59 +0000 | [diff] [blame] | 29 | return wchan; |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 30 | t = state[substr(s, 1, 1)]; |
| 31 | if(t == "") |
| 32 | return s; |
| 33 | return t; |
| 34 | } |
| 35 | |
| 36 | # rsc 36706 starttime 0:00.17 1076 Is+ -bash (bash) |
| 37 | { |
| 38 | i=1 |
| 39 | user=$i; i++ |
| 40 | pid=$i; i++ |
| 41 | start=$i; i++ |
| 42 | if(start ~ /^[A-Z][a-z][a-z]$/){ |
| 43 | start = start "-" $i; i++ |
| 44 | } |
| 45 | cputime=$i; i++ |
| 46 | mem=$i; i++ |
| 47 | stat=$i; i++ |
rsc | c57a737 | 2004-04-19 05:51:59 +0000 | [diff] [blame] | 48 | wchan=$i; i++ |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 49 | cmd=$i; i++ |
| 50 | if(ENVIRON["all"] == "yes"){ |
| 51 | for(; i<=NF; i++) |
| 52 | cmd = cmd " " $i; |
| 53 | }else{ |
| 54 | sub(/.*\//, "", cmd); |
| 55 | sub(/:$/, "", cmd); |
| 56 | sub(/^-/, "", cmd); |
| 57 | s = " " cmd; |
| 58 | } |
| 59 | sub(/\.[0-9][0-9]$/, "", cputime); # drop .hundredths of second |
| 60 | if(cputime ~ /..:..:../){ # convert hh:mm:ss into mm:ss |
| 61 | split(cputime, a, ":"); |
| 62 | cputime = sprintf("%d:%02d", a[1]*60+a[2], a[3]); |
| 63 | } |
| 64 | if(start ~ /..:..:../){ # drop :ss |
| 65 | sub(/:..$/, "", start); |
| 66 | } |
rsc | fc165dc | 2005-10-19 03:15:30 +0000 | [diff] [blame] | 67 | sub(/[ ]+$/, "", cmd); |
| 68 | line[0+nline++] = sprintf("%s\001%d\001%s\001%s\001%dK\001%s\001%s", |
rsc | c57a737 | 2004-04-19 05:51:59 +0000 | [diff] [blame] | 69 | user, pid, start, cputime, mem, statestr(stat, wchan), cmd); |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 70 | } |
rsc | fc165dc | 2005-10-19 03:15:30 +0000 | [diff] [blame] | 71 | |
| 72 | END{ |
| 73 | for(i=0; i<nline; i++){ |
| 74 | split(line[i], a, "\001"); |
| 75 | for(j=1; j<=7; j++) |
| 76 | if(length(a[j]) > max[j]) |
| 77 | max[j] = length(a[j]); |
| 78 | } |
| 79 | for(i=0; i<nline; i++){ |
| 80 | split(line[i], a, "\001"); |
| 81 | printf("%-*s %*s %*s %*s %*s %-*s %s\n", |
| 82 | max[1], a[1], max[2], a[2], max[3], a[3], max[4], a[4], |
| 83 | max[5], a[5], max[6], a[6], a[7]); |
| 84 | } |
| 85 | } |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 86 | ! |
| 87 | |
rsc | cb27443 | 2004-03-25 23:03:22 +0000 | [diff] [blame] | 88 | case "${SYSNAME:-`uname`}" in |
| 89 | SunOS) |
rsc | 3351416 | 2005-01-04 21:14:58 +0000 | [diff] [blame] | 90 | /bin/ps -aA -o 'user,pid,stime,time,rss,s,s,args' | sed 1d | |
rsc | cb27443 | 2004-03-25 23:03:22 +0000 | [diff] [blame] | 91 | nawk -f /tmp/awk.xxx$$ | sort -n +1 |
| 92 | ;; |
| 93 | *) |
rsc | 3351416 | 2005-01-04 21:14:58 +0000 | [diff] [blame] | 94 | /bin/ps -axww -o 'user,pid,start,time,rss,stat,wchan,command' | sed 1d | |
rsc | cb27443 | 2004-03-25 23:03:22 +0000 | [diff] [blame] | 95 | awk -f /tmp/awk.xxx$$ | sort -n +1 |
| 96 | ;; |
| 97 | esac |
rsc | 629864f | 2003-10-13 18:25:28 +0000 | [diff] [blame] | 98 | |
| 99 | rm -f /tmp/awk.xxx$$ |
| 100 | |