0

I need create a shell script to list the process by status type. The output must be something like:

Process running:

[process]

Process sleeping:

[process]

ETC

I did this, but doesnt work the ps aux | awk '$8 ~ PROCESS':

for PROCESS in `ps -v | awk 'NR!=1 {print $2}' | sort -u`; do
   echo "Procesos como $PROCESS:"
   ps aux | awk '$8 ~ PROCESS'
done

Cause that script outputs all the process, not filter by Process.

Any help?

Alberto Fortes
  • 562
  • 4
  • 13

2 Answers2

1

A simple solution would be to use ps and sort:

ps u | sort -rk 8

-r reverses the sort (so that the list header remains above), and -k 8 selects the 8th field (STAT).

You can then select processes in a specific state using anything form head to awk, and print out whatever you like.

salezica
  • 74,081
  • 25
  • 105
  • 166
0

You can also use top, in non-interactive mode ( the -S option to display and sort by state):

top -b -n 1 -S
b2Wc0EKKOvLPn
  • 2,054
  • 13
  • 15
  • But How I pass the variable for each process? I have to use awk after. the real problem is: awk '$8 ~ PROCESS' doesnt work – Alberto Fortes Apr 01 '13 at 19:10
  • The command below works pretty fine for me, it extracts the pid, the state, and the process name: `top -b -n 1 -S | awk '{print $1 " " $8 " " $12}'` – b2Wc0EKKOvLPn Apr 01 '13 at 19:48