Skip to content

Aldaviva/PowerCycler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

21 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

PowerCycler

GitHub Workflow Status

Automatically turn a power outlet off and on again when a health check fails.

Prerequisites

Installation

Pre-built executables are provided for Windows (x64) and Linux (x64 and ARM).

Linux

Tested on a Raspberry Pi 2 Model B running Raspbian 11 Bullseye.

  1. Download the Linux ARM build from the latest release.
  2. Unzip and move the executable.
    unzip PowerCycler-linux-arm.zip
    sudo mv PowerCycler /usr/local/bin/
    sudo chmod +x /usr/local/bin/PowerCycler
  3. Move the configuration file.
    sudo mv powercycler.json /usr/local/etc/
  4. Install the systemd service unit file.
    sudo mv powercycler.service /etc/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable powercycler.service

Windows

  1. Download the Windows build from the latest release.
  2. Unzip it to a folder such as C:\Program Files\PowerCycler\.
  3. Open PowerShell elevated and run
    New-Service -Name "PowerCycler" -DisplayName "PowerCycler" -Description "Turn it off and on again." -BinaryPathName "C:\Program Files\PowerCycler\PowerCycler.exe" -DependsOn Tcpip

Configuration

Edit the powercycler.json configuration file.

All fields are required.

Field Example value Description
healthCheckUrl https://aldaviva.com
or
tcp://aldaviva.com:22
The URL of the process being monitored to check and see if it's healthy.
For http and https schemes, it sends a GET request and requires a status code in [200, 300). Redirections are not followed.
For the tcp scheme, it opens a socket connection to the URL's hostname and port.
healthCheckTimeoutSec 5 How long, in seconds, the process being monitored has to send a health check response before that check fails. Should be shorter than healthCheckFrequencySec.
healthCheckFrequencySec 60 How often, in seconds, to send health check requests.
outletHostname bragi.outlets.aldaviva.com
or
192.168.1.100
The FQDN or IP address of a Kasa smart outlet to turn off and on when the process is deemed to be offline and must be restarted.
minOfflineDurationBeforeRestartSec 600 How long, in seconds, to go without any successful health check responses before the outlet is power cycled. Should be longer than healthCheckFrequencySec.
resumeHealthCheckAfterRestartSec 300 How long, in seconds, to wait after power cycling before checking health again. Should be longer than it takes your process to become healthy after a reboot.
Logging.LogLevel.Default Information Minimum severity of log events to emit. Values are Trace, Debug, Information, Warning, Error, Critical, and None.
View logs using sudo journalctl -u powercycler on Linux or Event Viewer on Windows.

With the above example values, this service will send an HTTP GET request to https://aldaviva.com every 60 seconds.

  • If it has returned at least one 200 OK response in the last 600 seconds, this service will do nothing.
  • Otherwise, if it has done nothing in the past 600 seconds but return errors (such as 400, 500, or 503) or take more than 5 seconds to respond, then this service will turn off the outlet at 192.168.1.100, wait 2 seconds, and then turn the outlet back on again. This service will then wait 300 seconds before finally starting the health check loop again.

Running

Linux

sudo systemctl start powercycler.service

Windows

Start-Service PowerCycler