How to get the number of running, sleeping, stoppe

2019-07-15 02:50发布

I am new to Unix, Linux, and shell scripting.
I need to know how to find the number of running, sleeping, stopped, and zombie processes.

I think I found some ways to find the number of running processes:

ps -ef | wc -l
ps r | wc -l

But I'm not sure which is better, or if either is a good way to do this.

When it comes to sleeping, stopped, and zombie though, I have no clue where to go.

Any help would be appreciated.

标签: linux shell
1条回答
Emotional °昔
2楼-- · 2019-07-15 02:59

Easy way, use top in one-shot mode:

$ top -bn1 | grep zombie
Tasks:  96 total,   1 running,  90 sleeping,   5 stopped,   0 zombie

You can then use awk to pull out the individual numbers, if that's more palatable:

$ top -bn1 | grep zombie | awk '{print $4" "$6" "$8" "$10}'
1 90 5 0

If you happen to have a process named "zombie" this recipe will run afoul. You might want to replace grep zombie with sed -n 2p.

查看更多
登录 后发表回答