blob: d6f44d9bb4f85a1bc60226a53661198c5f602562 [file] [log] [blame]
rsc629864f2003-10-13 18:25:28 +00001#!/bin/sh
2
3I_WANT_A_BROKEN_PS=yes
4export I_WANT_A_BROKEN_PS
5all=no
6if [ "x$1" = "x-a" ]
7then
8 all=yes
9fi
10export all
11
12cat >/tmp/awk.xxx$$ <<'!'
13BEGIN{
14 state["D"] = "Spinwait";
15 state["I"] = "Idle";
16 state["J"] = "Jail";
17 state["R"] = "Ready";
rsccb274432004-03-25 23:03:22 +000018 state["O"] = "Running";
rsc629864f2003-10-13 18:25:28 +000019 state["S"] = "Sleep";
20 state["T"] = "Stopped";
21 state["Z"] = "Zombie";
22 state["W"] = "Fault";
23 state["X"] = "Moribund";
24}
25
rscc57a7372004-04-19 05:51:59 +000026function statestr(s, wchan)
rsc629864f2003-10-13 18:25:28 +000027{
rsc33514162005-01-04 21:14:58 +000028 if(wchan != s && wchan != "?" && wchan != "-")
rscc57a7372004-04-19 05:51:59 +000029 return wchan;
rsc629864f2003-10-13 18:25:28 +000030 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++
rscc57a7372004-04-19 05:51:59 +000048 wchan=$i; i++
rsc629864f2003-10-13 18:25:28 +000049 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 }
rscfc165dc2005-10-19 03:15:30 +000067 sub(/[ ]+$/, "", cmd);
68 line[0+nline++] = sprintf("%s\001%d\001%s\001%s\001%dK\001%s\001%s",
rscc57a7372004-04-19 05:51:59 +000069 user, pid, start, cputime, mem, statestr(stat, wchan), cmd);
rsc629864f2003-10-13 18:25:28 +000070}
rscfc165dc2005-10-19 03:15:30 +000071
72END{
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}
rsc629864f2003-10-13 18:25:28 +000086!
87
rsccb274432004-03-25 23:03:22 +000088case "${SYSNAME:-`uname`}" in
89SunOS)
rsc33514162005-01-04 21:14:58 +000090 /bin/ps -aA -o 'user,pid,stime,time,rss,s,s,args' | sed 1d |
rsccb274432004-03-25 23:03:22 +000091 nawk -f /tmp/awk.xxx$$ | sort -n +1
92 ;;
93*)
rsc33514162005-01-04 21:14:58 +000094 /bin/ps -axww -o 'user,pid,start,time,rss,stat,wchan,command' | sed 1d |
rsccb274432004-03-25 23:03:22 +000095 awk -f /tmp/awk.xxx$$ | sort -n +1
96 ;;
97esac
rsc629864f2003-10-13 18:25:28 +000098
99rm -f /tmp/awk.xxx$$
100