批处理文件重新启动基于一个进程的CPU上的特定服务 [英] Batch file to restart a specific service based on the CPU of a process

查看:439
本文介绍了批处理文件重新启动基于一个进程的CPU上的特定服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你能帮助我,我已经试过研究了几个小时,但似乎无法用它来获取任何地方。

I'm hoping you can help me with this, I've tried researching for hours but can't seem to get anywhere with it.

我有一个随机开始消耗50+ CPU几次一天一个过程,我试图创建将在后台运行,如果该进程的CPU占用率得到50以上重新启动该服务的批处理文件(修复该问题)。

I have a process which randomly starts to consume 50+ CPU a few times a day, I'm trying to create a batch file which will run in the background and restart the service if the CPU usage of the process gets above 50 (fixes the issue).

我发现了一些关于这个论坛非常接近,一个名为电力币黑客的人回答,但他的回答是整体的CPU使用率,不是一个具体的过程只是用法。下面是他建议的批处理文件:

I found something very close on this forum, a guy named "Elektro Hacker" answered, but his answer is the overall CPU usage, not just the usage of a specific process. Here is the batch file he suggested:

@Echo OFF

SET    "SERVICE=Themes"
SET /A "MAXUSAGE=95"
SET /A "INTERVAL=5"

:LOOP 
For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do (
    IF %%P GTR %MAXUSAGE% (
        Echo [%TIME:~0,8%] CPU Usage: %%P%% Reached the limit: %MAXUSAGE%%%
        Echo Restarting %SERVICE% ...
         SC STOP  "%SERVICE%" 1>NUL
        SC START "%SERVICE%" 1>NUL
        Echo Service restarted.
    ) ELSE (
         Echo [%TIME:~0,8%] CPU Usage: %%P%%
    )
)
Ping -n %INTERVAL% Localhost >NUL
GOTO :LOOP

他还提出:欲了解更多precission也许你要检查使用该服务关联的可执行文件的当前CPU的百分比:

He also put: "For more precission maybe you want to check the current CPU percentage of the executable associated to the service using:

wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessor
Time

这是我不能让过去的一部分,我不断收到错误,每次我试图通过稍微改变它来解决它的时候,我得到不同的错误!

This is the part I can't get past, I keep getting errors, and every time I try to fix it by changing it slightly, I get different errors!

任何人可以改变这个code咯,得到它得到说MyProcess的CPU
这个过程和服务名称是不同的,我不知道这是否会有所作为,但认为这是值得一提。

Can anybody change this code slightly, to get it to get the CPU of say "MyProcess" The process and service names are different, I'm not sure if this would make a difference but thought it was worth mentioning.

我的编码知识是最小的,充其量,所以任何帮助或建议,您可以给我大大AP preciated,这是推动我疯了!

My coding knowledge is minimal at best, so any help or advice you could give me is greatly appreciated, this is driving my crazy!

非常感谢!

推荐答案

Powesshell可以做的更好。

Powesshell can do better

get-process wmiprvse | select processname,cpu,id | ? {$_.cpu -gt 90 -and $_.id} | foreach { restart-service winmgmt }

这篇关于批处理文件重新启动基于一个进程的CPU上的特定服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆