Linux 用户使用 bash 运行的进程数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3874677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The number of processes a user is running using bash
提问by Vidi
I would like to know how I could get the number of processes for each user that is currently logged in.
我想知道如何获取当前登录的每个用户的进程数。
采纳答案by Paused until further notice.
Give this a try:
试试这个:
ps -u "$(echo $(w -h | cut -d ' ' -f1 | sort -u))" o user= | sort | uniq -c | sort -rn
In order to properly handle usernames that may be longer than eight characters, use users
instead of w
. The latter truncates usernames.
为了正确处理可能超过八个字符的用户名,请使用users
代替w
。后者截断用户名。
ps -u "$(echo $(printf '%s\n' $(users) | sort -u))" o user= | sort | uniq -c | sort -rn
回答by Helmut Grohne
You could try some variation of this:
您可以尝试一些变体:
ps haux Ou | cut '-d ' -f1 | uniq -c
It gives you the number of processes for each users (being logged in or not). Now you could filter those results using the output of the w command or another way of determining who is logged in.
它为您提供每个用户的进程数(已登录或未登录)。现在,您可以使用 w 命令的输出或其他确定登录者的方式来过滤这些结果。
回答by Aboelnour
ps -u aboelnour | awk 'END {print NR}'
will show number of process which user aboelnour running it
将显示用户 aboelnour 运行它的进程数
回答by Naveed
Following links contain useful ps commands optionsincluding your requirements:
以下链接包含有用的ps 命令选项,包括您的要求:
回答by ghostdog74
userlist=$(w|awk 'BEGIN{ORS=","}NR>2{print }'|sed 's/,$//' )
ps -u "$userlist"
回答by user340140
If you just want a count of processes you can use procfs directly like this: (requires linux 2.2 or greater)
如果您只想计算进程数,您可以像这样直接使用 procfs:(需要 linux 2.2 或更高版本)
you can use wc:
你可以使用厕所:
number_of_processes=`echo /proc/[0-9]* | wc -w`
or do it in pure bash (no external commands) like this
或者像这样在纯 bash(没有外部命令)中进行
procs=( /proc/[0-9]* )
number_of_proccesses=${#procs[*]}
If you only want the current userid
如果您只想要当前的用户 ID
procs=( /proc/[0-9]*/fd/. )
number_of_proccesses=${#procs[*]}
回答by Rick
Here is my solution, for Linux:
这是我的解决方案,适用于 Linux:
$ find /proc –user $USER -maxdepth 1 -name '[0-9]*' | wc –l
$ find /proc –user $USER -maxdepth 1 -name '[0-9]*' | wc –l
This solution will not fail when the number of processes is larger than the command line limit.
当进程数大于命令行限制时,此解决方案不会失败。
回答by JoeG
If you are ever concerned about nearing the user process limit shown by ulimit -a
, the you want to get ALL the processes (including LWPs). In such a case you should use:
如果您担心接近 显示的用户进程限制ulimit -a
,则您希望获得所有进程(包括 LWP)。在这种情况下,您应该使用:
ps h -Led -o user | sort | uniq -c | sort -n
On one system doing this:
在一个系统上这样做:
ps haux Ou | cut '-d ' -f1 | uniq -c
yields:
产量:
# ps haux Ou | cut '-d ' -f1 | uniq -c
30 user1
1 dbus
3 user2
1 ntp
1 nut
1 polkitd
2 postfix
124 root
2 serv-bu+
where doing the former yields the true process count:
哪里做前者会产生真正的进程数:
# ps h -Led -o user | sort | uniq -c | sort -n
1 ntp
1 nut
2 dbus
2 postfix
2 serv-builder
3 user2
6 polkitd
141 root
444 user1