Linux中进程的空闲时间

发布于 2024-12-29 14:48:07 字数 299 浏览 0 评论 0原文

我需要计算Linux中进程的CPU使用率(用户模式、系统模式、空闲时间)。 我可以使用 /proc/PID/stat 中的 utimestime 值计算用户和系统模式下的使用情况,但我没有发现任何内容与闲置时间有关。

我知道我可以从 /proc/stat 获取空闲时间,但该值与机器相关,而不是与特定进程相关。

是否可以计算知道其 PID 的进程的空闲时间(从 /proc 目录读取数据)?

I need to calculate CPU usage (user mode, system mode, idle time) of a process in Linux.
I am able to calculate usage in user and system mode using utime and stime values from /proc/PID/stat, but I found nothing which is related to idle time.

I know I can get idle time from /proc/stat but this value is related to machine, not for particular process.

Is it possible to calculate idle time of a process knowing its PID (reading data from /proc directory)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和我恋爱吧 2025-01-05 14:48:07

我对此了解不多,但也许以下有效:

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

I don't know much about it but maybe the following works:

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))

Hope this helps! :D

-小熊_ 2025-01-05 14:48:07

已经太晚了,但是,我猜这个命令很有用:

IFS=

在 bash 文件中使用它:

#!/bin/bash
IFS=

您可以根据需要更改 grep -v "grep\|root"
这一行命令列出了非 root 所有者或系统用户的所有进程。

\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 文件中使用它:


您可以根据需要更改 grep -v "grep\|root"
这一行命令列出了非 root 所有者或系统用户的所有进程。

\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 所有者或系统用户的所有进程。

\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 文件中使用它:

您可以根据需要更改 grep -v "grep\|root"
这一行命令列出了非 root 所有者或系统用户的所有进程。

it's too late but, I guessed this command useful:

IFS=

to use it in bash file:

#!/bin/bash
IFS=

you could change grep -v "grep\|root" as you wish.
this one line command list all processes which not root owner or system users.

\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

to use it in bash file:


you could change grep -v "grep\|root" as you wish.
this one line command list all processes which not root owner or system users.

\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

you could change grep -v "grep\|root" as you wish.
this one line command list all processes which not root owner or system users.

\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

to use it in bash file:

you could change grep -v "grep\|root" as you wish.
this one line command list all processes which not root owner or system users.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文