Skip to content

Instantly share code, notes, and snippets.

Created August 15, 2022 17:50
What would you like to do?
Wake-On-LAN (WOL) Scripts - Fall Asleep If No Time Machine Backup Is Running on /mnt and Nobody Is Streaming via Plex
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root";
exit 1;
while true
DATE_AND_TIME=$(date '+%Y-%m-%d %H:%M:%S');
echo "### $DATE_AND_TIME"
NUMBER_OPEN_FILES="$(lsof -w /mnt/* | grep /mnt/ | wc -l)"
NUMBER_OF_TRANSCODES="$(ps aux | grep '/usr/lib/plexmediaserver/Plex Transcoder' | grep -v grep | wc -l)"
if [ "$NUMBER_OPEN_FILES" -eq "0" ] && [ "$NUMBER_OF_TRANSCODES" -eq "0" ]; then
echo "No files open AND no one is watching on Plex!";
echo "Going to sleep now, good night! zzzZZZ...";
systemctl suspend;
echo "Cannot sleep, $NUMBER_OPEN_FILES file(s) still open AND $NUMBER_OF_TRANSCODES transcode(s) active on Plex.";
echo "Next update in 10 minutes...";
sleep 600;
Description=Enable Sleeper Script
ExecStart = /home/<your username>/
if (!empty($_GET['act'])) {
// This is evil, we do not allow any user input here!
shell_exec('./wakeonlan 00:AA:BB:CC:DD:EE'); // Change Me
header('Location: '.'https://<your.hostname>/wol/'); // Change Me
$info = json_decode(getStateFromHASS(), true);
$state = $info['state'];
$last_changed = parseDate($info['last_changed']);
function getStateFromHASS() {
$ch = curl_init();
// Hostname
curl_setopt($ch, CURLOPT_URL, 'https://<your.home.assistant.hostname>/api/states/switch.wake_on_lan'); // Change Me
// Authentication
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer <Your Long-Lived Access Token>')); // Change Me
// Mute cURL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
function parseDate($dateStr) {
$dateStr = date_parse($dateStr);
$year = $dateStr['year'];
$month = str_pad($dateStr['month'], 2, '0', STR_PAD_LEFT);
$day = str_pad($dateStr['day'], 2, '0', STR_PAD_LEFT);
$hour = str_pad($dateStr['hour'], 2, '0', STR_PAD_LEFT);
$minute = str_pad($dateStr['minute'], 2, '0', STR_PAD_LEFT);
$second = str_pad($dateStr['second'], 2, '0', STR_PAD_LEFT);
$ts = gmmktime($hour, $minute, $second, $month, $day, $year);
return date("Y-m-d H:i:s", $ts);
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" href="favicon.ico">
.center {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
text-align: center;
font-size: 2rem;
width: 300px;
height: 100px;
<body id="main">
<div class="center">
<pre><?php echo 'Server is:<br>'.$state.'<br>'.'Last changed:<br>'.$last_changed;?>
<div class="center">
<form action="index.php" method="get">
<input type="hidden" name="act" value="run">
<input type="submit" name="submit_btn" class="button" value="Wake Up Server" />
Description=Enable Wake On Lan
ExecStart = /usr/sbin/ethtool --change <your LAN interface> wol g
Copy link

m33x commented Aug 15, 2022

During sleep (S3 -- Suspend to RAM) my 41 watts home server only draws 2 watts. I wrote a script so that it automatically falls asleep, if nobody uses it (for TimeMachine backups and Plex streaming). I can monitor its power consumption and state using Home Assistant and I even wrote a script, which can be used to wake it up again (in case I don't like to use Home Assistant for that).

Home Assistant UI:
Screenshot 2022-08-15 at 19 51 24
Screenshot 2022-08-15 at 19 56 35

Wakeup Script:
Screenshot 2022-08-15 at 19 35 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment