来自root 的进程对 CPU占用高解决方法

有时候,CPU 占用很高,而且是root的进程。比如 top

Tasks: 558 total,   1 running, 557 sleeping,   0 stopped,   0 zombie
%Cpu(s): 61.4 us,  0.0 sy,  0.0 ni, 38.6 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 13191446+total, 10841356+free,  3224532 used, 20276364 buff/cache
KiB Swap: 13409894+total, 13409894+free,        0 used. 12776337+avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                
 3430 root      20   0   81488  75840      4 S  3588  0.1 406637:57 sd-pam                                                                     
11707 user     20   0   40940   3980   3044 R   6.2  0.0   0:00.02 top                                                                    
    1 root      20   0  119924   6064   3968 S   0.0  0.0   0:11.58 systemd                                                                
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.10 kthreadd

这里 root 进程 sd-pam 占用了大量CPU资源。如何解决这个问题?

通过 pstree -p 3430 我们得到,该进程的完整的进程树是 systemd—libserv—libserv
通过 ps -eo pid,comm,unit 我们 得到 security info :

3430 libserv         cron.service

通过 pa -aux 我们得到 see every process on the system using BSD syntax:

root      1630  0.0  0.0  27728  2916 ?        Ss   Jun02   0:08 /usr/sbin/cron -f

我们找到,是cron 服务守护着 libserv 进程。这就是sd-pam 始终kill 不了的原因。
接下来的方法就比较简单了。

sudo kill 1630 
sudo kill 3430

相似的问题也发生在 sh,就是 top 列出的 sh 命令占用CPU很高,用上述方法可以解决。

参考:
a strange behavior of sd-pam #11753
Where is this cron job running from?
systemd System and Service Manager

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android设备上,即使没有root权限,你仍然可以使用Android API来获取CPU使用率。可以通过读取/proc/stat文件来获取CPU使用率信息。该文件包含了系统中各个进程以及CPU的使用情况,你可以通过分析该文件来计算CPU使用率。 具体来说,你可以使用以下步骤来获取CPU使用率: 1. 读取/proc/stat文件,获取CPU的总时间以及各个进程的时间信息。 2. 计算CPU的空闲时间和总时间,从而得到CPU的使用时间。 3. 计算CPU的使用率,即使用时间除以总时间。 以下是一个示例代码,用于获取CPU使用率: ```java public class CpuUsageMonitor { private static final String TAG = "CpuUsageMonitor"; private long lastCpuTime = 0; private long lastUpdateTime = 0; public float getCpuUsage() { try { FileReader fileReader = new FileReader("/proc/stat"); BufferedReader bufferedReader = new BufferedReader(fileReader); String line; while ((line = bufferedReader.readLine()) != null) { if (line.startsWith("cpu ")) { String[] cpuInfoArray = line.split("\\s+"); long userTime = Long.parseLong(cpuInfoArray[1]); long niceTime = Long.parseLong(cpuInfoArray[2]); long systemTime = Long.parseLong(cpuInfoArray[3]); long idleTime = Long.parseLong(cpuInfoArray[4]); long ioWaitTime = Long.parseLong(cpuInfoArray[5]); long irqTime = Long.parseLong(cpuInfoArray[6]); long softIrqTime = Long.parseLong(cpuInfoArray[7]); long totalTime = userTime + niceTime + systemTime + idleTime + ioWaitTime + irqTime + softIrqTime; if (lastCpuTime != 0) { long deltaTime = totalTime - lastCpuTime; long currentTime = System.currentTimeMillis(); long timeDelta = currentTime - lastUpdateTime; float cpuUsage = (deltaTime * 100.0f) / (timeDelta * 1000.0f * Runtime.getRuntime().availableProcessors()); return cpuUsage; } else { lastCpuTime = totalTime; lastUpdateTime = System.currentTimeMillis(); } } } bufferedReader.close(); fileReader.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read /proc/stat", ex); } return 0; } } ``` 在上述代码中,我们读取/proc/stat文件,解析出CPU的总时间以及各个进程的时间信息,然后计算出CPU的使用率。我们通过保存上一次读取的CPU时间和更新时间,来计算出两次读取之间的CPU使用时间。最后,我们将使用时间除以总时间,得到CPU的使用率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值