我需要计算 Linux 中一个进程的 CPU 使用率(用户模式、系统模式、空闲时间)。我可以使用utime
和stime
中的值来计算用户和系统模式下的使用情况/proc/PID/stat
,但我没有发现任何与空闲时间相关的内容。
我知道我可以从中获得空闲时间,/proc/stat
但这个值与机器有关,与特定进程无关。
是否可以计算知道进程的空闲时间PID
(从目录中读取数据/proc
)?
我需要计算 Linux 中一个进程的 CPU 使用率(用户模式、系统模式、空闲时间)。我可以使用utime
和stime
中的值来计算用户和系统模式下的使用情况/proc/PID/stat
,但我没有发现任何与空闲时间相关的内容。
我知道我可以从中获得空闲时间,/proc/stat
但这个值与机器有关,与特定进程无关。
是否可以计算知道进程的空闲时间PID
(从目录中读取数据/proc
)?
我对此了解不多,但也许以下工作:
1) Get the process start up time. Im sure thats possible
2) Generate time difference (dTime = CurrentTime - TimeProcessStarted)
3) Substract the time the process is running ( dTime - (usageSystemMode + usageUserMode))
希望这可以帮助!:D
为时已晚,但我猜这个命令很有用:
IFS=$'\n';for i in `ps -eo uname:20,pid,cmd | grep -v "USER\|grep\|root"`; \
do if [ $(id -g `echo $i | cut -d" " -f1`) -gt 1000 ] && \
[ $(echo $((($(date +%s) - $(date -d "$(ll -u \
--time-style=+"%y-%m-%d %H:%M:%S" /proc/$(echo $i | \
awk '{print $2}')/cwd | awk '{print $6" "$7}')" +%s))/3600))) >=1 ]; \
then echo $i; fi; done
在 bash 文件中使用它:
#!/bin/bash
IFS=$'\n'
for i in `ps -eo uname:20,pid,cmd | grep -v "USER\|grep\|root"`
do
Name="`echo $i | cut -d' ' -f1`"
Id="$(id -g $Name)"
Pid="`echo $i | awk '{print $2}'`"
Time1=$(date +%s)
Time2=$(date -d "$(/usr/bin/ls -lu --time-style=+"%y-%m-%d %H:%M:%S" \
/proc/$Pid/cwd | awk '{print $6" "$7}')" +%s)/3600
Time=$Time1-$Time2
if [ $Id -gt 1000 ] && [ $Time >=1 ]
then
echo $i
fi
done
您可以根据需要更改grep -v "grep\|root"。这一行命令列出了所有非 root 所有者或系统用户的进程。