Script on Ending Process according to memory consumption windows 10











up vote
0
down vote

favorite












I am looking for a batch or powershell script that if memory used by a process is less than 10 MB end that process and restart it.I tried searching a lot,but i could not find a ultimate solution.



This is the script i tried but it doesn't work. Please help, thanks!



:start
@ECHO OFF
SET procName=iexplorer.exe
SET RAMLimit=10240
FOR /F "tokens=*" %%F IN ('tasklist^|findstr %procName%') DO SET foundString=%%F
FOR /F "tokens=5" %%F IN ("%foundString%") DO SET RAMConsumption=%%F
IF %RAMConsumption% LEQ %RAMLimit% && ping 8.8.8.8 -n 6 | FIND /I "out"
if errorlevel 0 (
TASKKILL /IM %procName%
) else (
echo iexplorer is working
)
goto start









share|improve this question
























  • you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
    – SimonS
    Nov 13 at 8:56






  • 1




    i have added the script i tried.Thanks for advice.
    – Frankruss
    Nov 13 at 9:08










  • Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
    – Richard
    Nov 13 at 16:19

















up vote
0
down vote

favorite












I am looking for a batch or powershell script that if memory used by a process is less than 10 MB end that process and restart it.I tried searching a lot,but i could not find a ultimate solution.



This is the script i tried but it doesn't work. Please help, thanks!



:start
@ECHO OFF
SET procName=iexplorer.exe
SET RAMLimit=10240
FOR /F "tokens=*" %%F IN ('tasklist^|findstr %procName%') DO SET foundString=%%F
FOR /F "tokens=5" %%F IN ("%foundString%") DO SET RAMConsumption=%%F
IF %RAMConsumption% LEQ %RAMLimit% && ping 8.8.8.8 -n 6 | FIND /I "out"
if errorlevel 0 (
TASKKILL /IM %procName%
) else (
echo iexplorer is working
)
goto start









share|improve this question
























  • you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
    – SimonS
    Nov 13 at 8:56






  • 1




    i have added the script i tried.Thanks for advice.
    – Frankruss
    Nov 13 at 9:08










  • Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
    – Richard
    Nov 13 at 16:19















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am looking for a batch or powershell script that if memory used by a process is less than 10 MB end that process and restart it.I tried searching a lot,but i could not find a ultimate solution.



This is the script i tried but it doesn't work. Please help, thanks!



:start
@ECHO OFF
SET procName=iexplorer.exe
SET RAMLimit=10240
FOR /F "tokens=*" %%F IN ('tasklist^|findstr %procName%') DO SET foundString=%%F
FOR /F "tokens=5" %%F IN ("%foundString%") DO SET RAMConsumption=%%F
IF %RAMConsumption% LEQ %RAMLimit% && ping 8.8.8.8 -n 6 | FIND /I "out"
if errorlevel 0 (
TASKKILL /IM %procName%
) else (
echo iexplorer is working
)
goto start









share|improve this question















I am looking for a batch or powershell script that if memory used by a process is less than 10 MB end that process and restart it.I tried searching a lot,but i could not find a ultimate solution.



This is the script i tried but it doesn't work. Please help, thanks!



:start
@ECHO OFF
SET procName=iexplorer.exe
SET RAMLimit=10240
FOR /F "tokens=*" %%F IN ('tasklist^|findstr %procName%') DO SET foundString=%%F
FOR /F "tokens=5" %%F IN ("%foundString%") DO SET RAMConsumption=%%F
IF %RAMConsumption% LEQ %RAMLimit% && ping 8.8.8.8 -n 6 | FIND /I "out"
if errorlevel 0 (
TASKKILL /IM %procName%
) else (
echo iexplorer is working
)
goto start






windows-10 powershell batch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 9:14









SimonS

2,55831022




2,55831022










asked Nov 13 at 8:53









Frankruss

33




33












  • you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
    – SimonS
    Nov 13 at 8:56






  • 1




    i have added the script i tried.Thanks for advice.
    – Frankruss
    Nov 13 at 9:08










  • Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
    – Richard
    Nov 13 at 16:19




















  • you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
    – SimonS
    Nov 13 at 8:56






  • 1




    i have added the script i tried.Thanks for advice.
    – Frankruss
    Nov 13 at 9:08










  • Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
    – Richard
    Nov 13 at 16:19


















you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
– SimonS
Nov 13 at 8:56




you won't get an answer to a question like this, because superuser is not a free script writing service. Please show us your research and what problems you had implementing it, then we're glad to help.
– SimonS
Nov 13 at 8:56




1




1




i have added the script i tried.Thanks for advice.
– Frankruss
Nov 13 at 9:08




i have added the script i tried.Thanks for advice.
– Frankruss
Nov 13 at 9:08












Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
– Richard
Nov 13 at 16:19






Less than 10MB? I can understand wanting to restart processes which take up too much memory, but not too little. What are you trying to solve?
– Richard
Nov 13 at 16:19












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










This would be a PowerShell Solution:



Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}


first, it looks for all iexplore processes, then it filters in where all processes which have less or equal to 10MB RAM Consumption. For Each process that matched the where, it stops and re-starts the process



Edit: it looks like you want to run this in an infinite loop, if so, just wrap your script in a while loop like this



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}
sleep -s 1
}


if there's no path:



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Ping) {
Stop-Process $_ -Force
Start-Process iexplore
}
}
sleep -s 1
}





share|improve this answer























  • Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
    – Frankruss
    Nov 13 at 16:11










  • @Frankruss I added some logic, does it work properly now?
    – SimonS
    Nov 14 at 8:19










  • Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
    – Frankruss
    Nov 14 at 8:34










  • @Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
    – SimonS
    Nov 14 at 8:44








  • 1




    THNKS SIR.It is working now.
    – Frankruss
    Nov 14 at 11:09











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374961%2fscript-on-ending-process-according-to-memory-consumption-windows-10%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










This would be a PowerShell Solution:



Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}


first, it looks for all iexplore processes, then it filters in where all processes which have less or equal to 10MB RAM Consumption. For Each process that matched the where, it stops and re-starts the process



Edit: it looks like you want to run this in an infinite loop, if so, just wrap your script in a while loop like this



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}
sleep -s 1
}


if there's no path:



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Ping) {
Stop-Process $_ -Force
Start-Process iexplore
}
}
sleep -s 1
}





share|improve this answer























  • Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
    – Frankruss
    Nov 13 at 16:11










  • @Frankruss I added some logic, does it work properly now?
    – SimonS
    Nov 14 at 8:19










  • Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
    – Frankruss
    Nov 14 at 8:34










  • @Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
    – SimonS
    Nov 14 at 8:44








  • 1




    THNKS SIR.It is working now.
    – Frankruss
    Nov 14 at 11:09















up vote
1
down vote



accepted










This would be a PowerShell Solution:



Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}


first, it looks for all iexplore processes, then it filters in where all processes which have less or equal to 10MB RAM Consumption. For Each process that matched the where, it stops and re-starts the process



Edit: it looks like you want to run this in an infinite loop, if so, just wrap your script in a while loop like this



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}
sleep -s 1
}


if there's no path:



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Ping) {
Stop-Process $_ -Force
Start-Process iexplore
}
}
sleep -s 1
}





share|improve this answer























  • Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
    – Frankruss
    Nov 13 at 16:11










  • @Frankruss I added some logic, does it work properly now?
    – SimonS
    Nov 14 at 8:19










  • Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
    – Frankruss
    Nov 14 at 8:34










  • @Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
    – SimonS
    Nov 14 at 8:44








  • 1




    THNKS SIR.It is working now.
    – Frankruss
    Nov 14 at 11:09













up vote
1
down vote



accepted







up vote
1
down vote



accepted






This would be a PowerShell Solution:



Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}


first, it looks for all iexplore processes, then it filters in where all processes which have less or equal to 10MB RAM Consumption. For Each process that matched the where, it stops and re-starts the process



Edit: it looks like you want to run this in an infinite loop, if so, just wrap your script in a while loop like this



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}
sleep -s 1
}


if there's no path:



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Ping) {
Stop-Process $_ -Force
Start-Process iexplore
}
}
sleep -s 1
}





share|improve this answer














This would be a PowerShell Solution:



Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}


first, it looks for all iexplore processes, then it filters in where all processes which have less or equal to 10MB RAM Consumption. For Each process that matched the where, it stops and re-starts the process



Edit: it looks like you want to run this in an infinite loop, if so, just wrap your script in a while loop like this



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
$Path = $_.Path
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Path -and $Ping) {
Stop-Process $_ -Force
Start-Process $Path
}
}
sleep -s 1
}


if there's no path:



while ($true) {
Get-Process iexplore -ea 0 | where { $_.PM -le 10MB } | foreach {
[bool]$Ping = Test-Connection 8.8.8.8 -Quiet
if ($Ping) {
Stop-Process $_ -Force
Start-Process iexplore
}
}
sleep -s 1
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 at 10:36

























answered Nov 13 at 10:26









SimonS

2,55831022




2,55831022












  • Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
    – Frankruss
    Nov 13 at 16:11










  • @Frankruss I added some logic, does it work properly now?
    – SimonS
    Nov 14 at 8:19










  • Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
    – Frankruss
    Nov 14 at 8:34










  • @Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
    – SimonS
    Nov 14 at 8:44








  • 1




    THNKS SIR.It is working now.
    – Frankruss
    Nov 14 at 11:09


















  • Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
    – Frankruss
    Nov 13 at 16:11










  • @Frankruss I added some logic, does it work properly now?
    – SimonS
    Nov 14 at 8:19










  • Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
    – Frankruss
    Nov 14 at 8:34










  • @Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
    – SimonS
    Nov 14 at 8:44








  • 1




    THNKS SIR.It is working now.
    – Frankruss
    Nov 14 at 11:09
















Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
– Frankruss
Nov 13 at 16:11




Thanku sir.Thanks a lot.i am almost there but there is some problem with ur script.I wanted to check for a process continuosly that if the memory gets less than 10 MB the application restarts.The process is getting restarted as per ur script.but if the memory used is more than 10 MB than its getting stucked.And last thing sir i want to also add a condition that restart the process only if less than 10 MB and internet is working.Like when this ping 8.8.8.8 is true.I could do cmd but i dont know mucj=h in powershell.THANKS SIR.
– Frankruss
Nov 13 at 16:11












@Frankruss I added some logic, does it work properly now?
– SimonS
Nov 14 at 8:19




@Frankruss I added some logic, does it work properly now?
– SimonS
Nov 14 at 8:19












Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
– Frankruss
Nov 14 at 8:34




Sir thanks for ur support.I wanted that if both condition is true i.e. memory less than 10MB and internet does not work then only restart the process.Ur logic restarts the process only if internet is working and memory condition is true.but when i disconnect the internet it closes the process but does not restart it again.THANKS.
– Frankruss
Nov 14 at 8:34












@Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
– SimonS
Nov 14 at 8:44






@Frankruss ok, now it should behave the way you want it to. even though i did not understand the internet part. it would restart now if ping can reach 8.8.8.8. if you want to restart if ping does NOT reach 8.8.8.8, change if ($Path -and $Ping) to if ($Path -and !$Ping)
– SimonS
Nov 14 at 8:44






1




1




THNKS SIR.It is working now.
– Frankruss
Nov 14 at 11:09




THNKS SIR.It is working now.
– Frankruss
Nov 14 at 11:09


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1374961%2fscript-on-ending-process-according-to-memory-consumption-windows-10%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...