Skip to content

Instantly share code, notes, and snippets.

@nomayann
Last active October 1, 2021 10:43
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 nomayann/98c28213d5975d1d4837aa513fcd69fd to your computer and use it in GitHub Desktop.
Save nomayann/98c28213d5975d1d4837aa513fcd69fd to your computer and use it in GitHub Desktop.
Energy saving script: stops automatically a NAS server after an inactivity delay (no ssh client, no samba client as well).
#!/bin/bash
## Cron this every minute so that it runs as root
FLAG_FILE="/home/yann/.no_client.flg"
SVG_FLAG="/home/yann/.backup.flg"
SHUTDOWN_DELAY=15
# Remove file flag if any user is connected and exit
if [ $(who -T | wc -l) != 0 ]; then
rm -f $FLAG_FILE
logger "Auto-Stop : There are users connected."
exit 0
fi
# Remove file flag if any samba client is connected and exit
if [ $(smbstatus --shares | grep 192 | wc -l) != 0 ]; then
rm -f $FLAG_FILE
logger "Auto-Stop : There are samba clients."
exit 0
fi
echo $(date) - there is no activity. Touching the flag. >> $LOG_FILE
touch -a $FLAG_FILE
# Skip if backup is on the run
if [ -f $SVG_FLAG ]; then
logger "Auto-Stop : Backup is on the run."
exit 0
fi
# Stop the system automatically if the flag is older than X minutes
if test `find $FLAG_FILE -mmin +$SHUTDOWN_DELAY`; then
logger "Auto-Stop : Stopping system. There was no client for $SHUTDOWN_DELAY minutes."
rm -f $FLAG_FILE
/sbin/shutdown -P now
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment