Skip to content

Instantly share code, notes, and snippets.

@bryanvine
Last active April 20, 2020 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanvine/b202fe7e649be28d1288579a98e3d284 to your computer and use it in GitHub Desktop.
Save bryanvine/b202fe7e649be28d1288579a98e3d284 to your computer and use it in GitHub Desktop.
Auto Restart Folding@Home
<#
Name: Auto Restart Folding@Home
Author: Bryan Vine
Date: 3/25/2020
Description: Automatically restarts the local Folding @ Home client while cleaning out existing work to reset the connection countdown timer based on CPU & GPU usage.
Link: https://www.bryanvine.com/2020/03/covid-19-finding-cure-with-foldinghome.html
#>
#Requires -Version 3.0
$restart_counter = 1
#Loop forever
while($true){
$CPU_usage = (Get-Counter '\Processor(_Total)\% Processor Time').countersamples.cookedvalue
$GPU_usage = (Get-Counter -Counter "\GPU Engine(*)\Utilization Percentage").countersamples.cookedvalue | sort -Descending | select -first 1
"$(Get-Date) - CPU usage: $([math]::Round($CPU_usage,0))%, GPU usage: $([math]::Round($GPU_usage,0))%"
#Resets if CPU or GPU are under 20%
if($CPU_usage -lt 20 -or $GPU_usage -lt 20){
"CPU and GPU idle, restarting F@H for the $restart_counter time"
$restart_counter++
if(Get-Service -name "*Folding@Home*"){
Get-Service -name "*Folding@Home*" | Stop-Service -Verbose
}else{
Get-Process -Name FAHClient -ErrorAction SilentlyContinue| Stop-Process -Verbose
}
Start-Sleep -Seconds 3
Push-Location
if(!(dir "C:\Users\$($env:USERNAME)\AppData\Roaming\FAHClient\work\" -directory)){
Remove-Item "C:\Users\$($env:USERNAME)\AppData\Roaming\FAHClient\work\client.db*" -Force -Verbose
}
if(Get-Service -name "*Folding@Home*"){
Get-Service -name "*Folding@Home*" | Start-Service -Verbose
Write-Verbose "Started FAHClient Service" -Verbose
}else{
cd "C:\Users\$($env:USERNAME)\AppData\Roaming\FAHClient"
. "C:\Program Files (x86)\FAHClient\HideConsole.exe" "C:\Program Files (x86)\FAHClient\FAHClient.exe"
Write-Verbose "Started FAHClient" -Verbose
}
Pop-Location
}
#wait 5 minutes
Start-Sleep -Seconds 300
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment