Hmm don't know what is the best forum for own scripts? like autoshutdown

HansBohne

Dabbler
Joined
Jan 7, 2024
Messages
15
As "development" seems to be for the core devs - so I don't want to interfere there.

I tried to make a script that shutdowns my TrueNAS Server after e.g. 30 mins no one accessed the files - but probably there is a better way than mine - I'm using "lsof" for example, which does it's job - but hmmm not ideal.

I know many use TrueNAS as an always on server - but I do not - the TrueNAS Server in our home runs on average under 1 hour a day - sometimes 10 days not at all
 

HansBohne

Dabbler
Joined
Jan 7, 2024
Messages
15
Hmmm maybe I should start with my script....

thats the script itself

Code:
#!/usr/bin/bash

. /root/custom/truenas_conf.txt

debugPrint "[checkpowerdown] Starte CheckPowerdown $0"

if [ "$myDebugOutput" = "/dev/null" ]; then
   myCheckInterval=$myCheckShutdownInterval  # in Sekunden
else
   myCheckInterval=3
fi


myTotalCheckTimeInSec=$(( $myCheckShutdownTime * 60 ))
myTotalCheckRuns=$(( $myTotalCheckTimeInSec / $myCheckInterval ))

myTotalTimesChecked=0

if [ "$myDebugOutput" = "/dev/null" ]; then
   sleep 180  # Mind 3 Minuten warten beim start
fi

while :

do

   /usr/bin/sleep $myCheckInterval

   myResult=$(lsof -w | grep "  REG  " | grep "^rsync" | grep -v "  mem  " | grep -v "  txt  " | grep -v "/tmp/rsyncd.pid")
   if ! [ -z "$myResult" ]; then
     debugPrint "[checkpowerdown] rsync Server uebertraegt Daten"
     myTotalTimesChecked=0
     continue
   fi      

   myResult=$(/root/custom/scripte/display/getisscrubbing.sh)
   if ! [ "$myResult" = "~" ]; then
      debugPrint "[checkpowerdown] Scrub laeuft"
      myTotalTimesChecked=0
      continue
   fi
   
   myResult=$(/root/custom/scripte/display/getzfsstatus.sh)
   if ! [ "$myResult" = "OK" ]; then
      debugPrint "[checkpowerdown] ZFS defekt"
      myTotalTimesChecked=0
      continue
   fi

   myResult=$(/usr/bin/pidof "mc-server-runner")
   if ! [ -z "$myResult" ]; then
      debugPrint "[checkpowerdown] Minecraft Server laeuft"
      myTotalTimesChecked=0
      continue
   fi

   if [ "$myDebugOutput" = "/dev/null" ]; then    
      myResult=$(/usr/bin/users)
      if ! [ -z "$myResult" ]; then
         debugPrint "[checkpowerdown] User angemeldet"         
         myTotalTimesChecked=0
         continue
      fi
   fi      
   
   myResult=$(/usr/bin/lsof -w | /usr/bin/grep "  REG  " | /usr/bin/grep "$myCheckShutdownDir" | /usr/bin/grep -v "/ix-applications/")
   if [ -z "$myResult" ]; then
      debugPrint "[checkpowerdown] Kann heruntergefahren werden"
      myTotalTimesChecked=$(( myTotalTimesChecked + 1 ))
      if [ $myTotalTimesChecked -gt $myTotalCheckRuns ]; then
         debugPrint "[checkpowerdown] Shutdown"
         /usr/bin/curl -s -X POST http://${myIP}/api/v2.0/system/shutdown -H "Authorization: Bearer $myAPIKey"
         exit 0
      fi
   else
      debugPrint "[checkpowerdown] Es sind Dateien offen"
      myTotalTimesChecked=0
   fi
done



and the config file

Code:
myAPIKey="11111111222222222222222223333333333333333333"
myCheckShutdownDir="/mnt/tank"
myCheckShutdownTime=60    # in Minuten
myCheckShutdownInterval=30  # in Sekunden
myDebugOutput="/dev/null"   # DebugOn: "/tmp/status.txt"     
checkShutdownFlag="/tmp/shutdown.go"


debugPrint() {
   if ! [ -z "$1" ]; then
      if ! [ "$myDebugOutput" = "/dev/null" ]; then
         echo "$1" >> "$myDebugOutput"
      fi
   fi
}


Checking of ZFS Health
Code:
#!/bin/bash

. "/root/custom/truenas_conf.txt"

myCommand=$(echo "curl -s -X GET \"http://$myIP/api/v2.0/pool\" -H \"Authorization: Bearer $myAPIKey\" | grep \"\\\""healthy\\\"": true\"")

myResult=$(bash -c "$myCommand")

if [ -z "$myResult" ]; then
   echo "ERROR"
   exit 1
fi

echo "OK"
exit 0


And of scrub state
Code:
#!/bin/bash

. "/root/custom/truenas_conf.txt"

myCommand=$(echo "curl -s -X GET \"http://$myIP/api/v2.0/pool\" -H \"Authorization: Bearer $myAPIKey\" | grep \"\\\""state\\\"": \\\""SCANNING\\\"",\"")
myResult=$(bash -c "$myCommand")

if [ -z "$myResult" ]; then
   echo "~"
   exit 1
fi

myCommand=$(echo "curl -s -X GET \"http://$myIP/api/v2.0/pool\" -H \"Authorization: Bearer $myAPIKey\" | grep \"\\\""percentage\\\"":\" | sed 's/\..*//' | sed 's/[^0-9]*//'")
myResult=$(bash -c "$myCommand")
if [ "$myResult" = "0" ]; then
   myResult=1
fi

echo "${myResult}%"

exit 0
 
Top