Skip to content

Instantly share code, notes, and snippets.

@m33x
Created Aug 15, 2022
Embed
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
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root";
exit 1;
fi
while true
do
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;
else
echo "Cannot sleep, $NUMBER_OPEN_FILES file(s) still open AND $NUMBER_OF_TRANSCODES transcode(s) active on Plex.";
fi
echo "Next update in 10 minutes...";
sleep 600;
done
# https://necromuralist.github.io/posts/using-systemd-to-enable-wake-on-lan/
[Unit]
Description=Enable Sleeper Script
[Service]
Type=simple
ExecStart = /home/<your username>/sleep.sh
PIDFile=/tmp/sleeper.pid
[Install]
WantedBy=basic.target
<?php
if (!empty($_GET['act'])) {
// This is evil, we do not allow any user input here!
// https://github.com/jpoliv/wakeonlan/blob/master/wakeonlan
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);
curl_close($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);
date_default_timezone_set('Europe/Berlin');
$ts = gmmktime($hour, $minute, $second, $month, $day, $year);
return date("Y-m-d H:i:s", $ts);
}
?>
<!doctype html>
<html lang="en">
<head>
<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">
<title>Wake-on-LAN</title>
<style>
.center {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
text-align: center;
}
input[name="submit_btn"]
{
font-size: 2rem;
width: 300px;
height: 100px;
}
</style>
</head>
<body id="main">
<div class="center">
<pre><?php echo 'Server is:<br>'.$state.'<br>'.'Last changed:<br>'.$last_changed;?>
</pre>
</div>
<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" />
</form>
</div>
</body>
</html>
# https://necromuralist.github.io/posts/using-systemd-to-enable-wake-on-lan/
[Unit]
Description=Enable Wake On Lan
[Service]
Type=oneshot
ExecStart = /usr/sbin/ethtool --change <your LAN interface> wol g
[Install]
WantedBy=basic.target
@m33x
Copy link
Author

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