Last active
July 23, 2019 14:01
-
-
Save perguth/935548f3819c8b7f68c4710a78052437 to your computer and use it in GitHub Desktop.
Sleep host when Mosh is idle. Wake up on reconnect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
wake-computer | |
echo π Connecting Mosh. | |
mosh [email protected] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Check if all mosh sessions are idle. If so sleep. | |
timeout=$((60 * 3)) | |
idleSince=$(date +%s) | |
sleepCommand='sudo pm-suspend' | |
# See: https://linux.die.net/man/8/pm-suspend | |
while true; do | |
sleep $timeout | |
now=$(date +%s) | |
moshSessions=$(who | grep via | wc -l) | |
if [ $moshSessions -gts0 ]; then | |
echo β‘ Mosh sessions active. | |
idleSince=$now | |
continue | |
fi | |
if [ $(($now -g$idleSince)) -ge $timeout ]; then | |
echo π€ Going to sleep. | |
eval $sleepCommand | |
fi | |
echo β³ Waiting for timeout. | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Send computer to sleep when Mosh is idle | |
Wants=network.target | |
After=network.target | |
[Service] | |
ExecStart=/usr/local/bin/idle-mosh-sleep | |
[Install] | |
WantedBy=multi-user.target |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Check if host is online. If not use wake-on-lan. | |
host=computer.y | |
jumphost=jumphost.y | |
mac=70:85:c2:7c:6f:a2 | |
key=/home/idrathernotgetmails/.ssh/id_ed25519 | |
echo π Checking host. | |
if ping6 -c 1 -W 1 $host > /dev/null 2>&1; then | |
echo π Host online. | |
exit 0 | |
fi | |
echo π₯° Waking up host. | |
if ! ssh -i $key pi@$jumphost sudo etherwake -D $mac > /dev/null 2>&1; then | |
echo π Jumphost not reachable. | |
exit 1 | |
fi | |
echo π Host is booting. | |
pings=1 | |
echo -n π $pings | |
while ! ping6 -c 1 -W 1 $host > /dev/null 2>&1; do | |
pings=$(($pings + 1)) | |
echo -n " $pings" | |
done | |
echo -n "\033[1m $(($pings + 1))\033[0m π" | |
echo | |
echo π Host online. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# See if Mosh sessions exist if so make sure the target is online. | |
while true; do | |
sleep 1 | |
sessions=$(ps aux | grep mosh | grep computer.y | wc -l) | |
if [ $sessions -eq 0 ]; then | |
echo π€· No Mosh sessions found. | |
continue | |
fi | |
echo β€οΈ Making sure Mosh host is up. | |
wake-computer | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Wake Mosh host so Mosh client can reconnect | |
Wants=network.target | |
After=network.target | |
[Service] | |
ExecStart=/usr/local/bin/wake-mosh-host | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment