Blob


1 #!/bin/sh
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
12 cat >/tmp/awk.xxx$$ <<'!'
13 BEGIN{
14 state["D"] = "Spinwait";
15 state["I"] = "Idle";
16 state["J"] = "Jail";
17 state["R"] = "Ready";
18 state["S"] = "Sleep";
19 state["T"] = "Stopped";
20 state["Z"] = "Zombie";
21 state["W"] = "Fault";
22 state["X"] = "Moribund";
23 }
25 function statestr(s)
26 {
27 t = state[substr(s, 1, 1)];
28 if(t == "")
29 return s;
30 return t;
31 }
33 # rsc 36706 starttime 0:00.17 1076 Is+ -bash (bash)
34 {
35 i=1
36 user=$i; i++
37 pid=$i; i++
38 start=$i; i++
39 if(start ~ /^[A-Z][a-z][a-z]$/){
40 start = start "-" $i; i++
41 }
42 cputime=$i; i++
43 mem=$i; i++
44 stat=$i; i++
45 cmd=$i; i++
46 if(ENVIRON["all"] == "yes"){
47 for(; i<=NF; i++)
48 cmd = cmd " " $i;
49 }else{
50 sub(/.*\//, "", cmd);
51 sub(/:$/, "", cmd);
52 sub(/^-/, "", cmd);
53 s = " " cmd;
54 }
55 sub(/\.[0-9][0-9]$/, "", cputime); # drop .hundredths of second
56 if(cputime ~ /..:..:../){ # convert hh:mm:ss into mm:ss
57 split(cputime, a, ":");
58 cputime = sprintf("%d:%02d", a[1]*60+a[2], a[3]);
59 }
60 if(start ~ /..:..:../){ # drop :ss
61 sub(/:..$/, "", start);
62 }
63 printf("%-8s %11d %8s %8s %8dK %-8s %s\n",
64 user, pid, start, cputime, mem, statestr(stat), cmd);
65 }
66 !
68 /bin/ps -axww -o 'user,pid,start,time,vsz,stat,command' | sed 1d |
69 awk -f /tmp/awk.xxx$$ | sort -n +1
71 rm -f /tmp/awk.xxx$$