haz*_*mat 41 linux bash ulimit
有什么方法可以检查给定用户的 ulimit 使用情况吗?我知道您可以在启动时更改单个进程的 ulimit,或者在运行时更改单个 shell,但我希望能够“监控”用户接近其限制的程度。我计划编写一个bash
脚本,将当前的使用百分比报告给 statsd。具体来说,我想跟踪:
ulimit -n
)ulimit -u
)ulimit -i
)我想要的是使用百分比(0-100)。
m13*_*13r 31
也许这有助于解决第一个问题:
如果您知道特定用户的进程 ID (PID),则可以通过以下方式获取每个进程的限制:
cat /proc/<PID>/limits
Run Code Online (Sandbox Code Playgroud)
您可以使用以下命令获取每个 PID 的打开文件数:
ls -1 /proc/<PID>/fd | wc -l
Run Code Online (Sandbox Code Playgroud)
然后只需将 的值Max open files
与第二个命令中打开的文件描述符的数量进行比较即可获得百分比。
agc*_*agc 18
通过几种不方便的方法和标准工具输出当前用户的打开文件、进程和挂起信号的百分比:
paste <(grep 'open files\|processes\|pending signals' \
/proc/self/limits | cut -c27-38) \
<(i=`whoami`
lsof -n -u $i 2> /dev/null |
tail -n +2 |
awk {'print $9'} |
wc -l
ps --no-headers -U $i -u $i u | wc -l
ps -u $i -o pid= |
xargs printf "/proc/%s/status\n" |
xargs grep -s 'SigPnd' |
sed 's/.*\t//' |
paste -sd+ |
bc ; ) \
<(grep 'open files\|processes\|pending signals' \
/proc/self/limits |
cut -c1-19) |
while read a b name ; do
printf '%3i%% %s\n' $((${b}00/a)) "$name"
done
Run Code Online (Sandbox Code Playgroud)
我的系统上的输出:
16% Max processes
12% Max open files
0% Max pending signals
Run Code Online (Sandbox Code Playgroud)
假设这些数字很好,这个答案表明它可以在 shell 中完成,但可能不应该,除非有更好的 shell 方法。相反,这种问答会更好做gcc
,或者python
,等。
sch*_*ily -8
ulimit 是进程的属性,并继承给子进程。
您无法获得另一个进程的限制。