Project

General

Profile

Powersave, Auto-shutdown, Auto-startup

Added by Dutchy Nl 4 months ago

To auto-shutdown and auto-startup a computer which only serves as a TvHeadend-server, the next script might be of some help.
What the script does for you is testing your system to shut off unless:

  • the system is running less than a given minimum time frame
  • you are watching TV
  • a recording is taking place
  • you are watching a recording
  • within a short time a new recording will start
  • you are logged in on the system
  • a post recording or any other process you specified is active

What the script also does for you, is switching your system on short before a new recording should start (default 3 minutes).
Example: program will start at 10:00, start ahead TvHeadend 5 mins, so wakeup is at 9:52 and recording can start at 9:55.

Some hints:
  • Be sure RTC is set (and stays set) to UTC-time.
  • Also be sure that the user who calls the script has rights to execute (see the wiki about “HowTo wakeup XBMC/TVHeadend for scheduled recording”).
  • Run the script with "sudo"-rights on boot. It will remain active until shutdown.
  • Install and use Webmin to manage your system and to program the script to run on boot the easy way.
    (Start webmin session > system > bootup and shutdown > create a new upstart service.)
  • Install all necessary packages like rtcwake.
#!/bin/bash
export LANG=C                          # SET LANGUAGE

# TEST 1 = NETWORK TRAFFIC MORE THAN MINIMUM (WHILE WATCHING A RECORDING FOR EXAMPLE) > STAY AWAKE
# TEST 2 = SERVER UP LESS THAN MINIMUM TIME SINCE BOOT > STAY AWAKE
# TEST 3 = ACTIVE USERS > STAY AWAKE
# TEST 4 = ACTIVE USER PROCESSES NOT TO BE INTERRUPTED > STAY AWAKE
# TEST 5 = WATCHING TV, RECORDING OR NEXT WAKE UP WITHIN SAFE MARGIN FROM NEXT EVENT > STAY AWAKE

# VARIABLES TO SET
INTERVAL=60                             # SECONDS TO WAIT FOR NEXT TESTRUN CHECKING FOR SHUTDOWN
NRtests=30                              # MINIMUM NUMBER OF TESTRUNS TO STAY AWAKE AFTER BOOT OR AFTER NETWORK TRAFFIC IDLE
NWinterface="eth0"                      # NETWORK INTERFACE TO MONITOR FOR TRAFFIC
INidle=100 && OUTidle=100               # MAXIMUM NETWORK TRAFFIC IN AND OUT CONSIDERED IDLE (IN KILOBITS)
PROCcheck=(ffmpeg avconv)               # PROCESSES TO BE CHECKED FOR ACTIVITY
MARGINshutdown=10                       # MINIMUM MINUTES FOR CONSECUTIVE SHUTDOWN AND STARTUP
MARGINstartup=3                         # MINIMUM MINUTES TO START UP PROPERLY
EPGhours=24                             # MAXIMUM HOURS NOT TO WAKE UP FOR UPDATING EPG
TVHnm="USER" && TVHpwd="PASSWORD"       # TVHEADEND LOGIN USER NAME AND PASSWORD

NETarray=( $(grep "$NWinterface" /proc/net/dev | awk '{print $2"="$10}') )                  # GET NETWORK STATE AND START ARRAY
testIN=$((NRtests*INTERVAL*1000*INidle/8)) && testOUT=$((NRtests*INTERVAL*1000*OUTidle/8))  # CALCULATE NETWORK TRAFFIC IN AND OUT TEST VALUES

while true; do                                                                              # INFINITE LOOP TESTING FOR SHUTDOWN
    sleep $INTERVAL                                                                         # WAIT TO START NEXT CHECK
    NETarray[${#NETarray[*]}]=$(grep "$NWinterface" /proc/net/dev | awk '{print $2"="$10}') # GET NETWORK STATE AND ADD TO ARRAY
    offset=$((${#NETarray[*]}-NRtests-1)) && NETarray=( ${NETarray[*]:${offset/-*/0}} )     # CLIP ARRAY ACCORDING TO TEST TIME FRAME; MIN ACCEPTABLE OFFSET ZERO
    if [ $((${NETarray[-1]/=*/}-${NETarray[0]/=*/})) -gt $testIN ]; then continue; fi       # TEST 1a NETWORK TRAFFIC IN BELOW MINIMUM
    if [ $((${NETarray[-1]/*=/}-${NETarray[0]/*=/})) -gt $testOUT ]; then continue; fi      # TEST 1b NETWORK TRAFFIC OUT BELOW MINIMUM
    if [ ${#netINarray[*]} -le $NRtests ]; then continue; fi                                # TEST 2 SERVER UP LESS THAN MINIMUM TIME SINCE BOOT
    if [[ "$(who)" != "" ]]; then continue; fi                                              # TEST 3 FOR ACTIVE USERS
    for i in ${PROCcheck[*]}; do                                                            # LOOP TESTING FOR ACTIVE PROCESSES
        if [[ "$(ps -A | grep "$i")" != "" ]]; then continue 2; fi                          # TEST 4 FOR ACTIVE PROCESSES AS SPECIFIED
    done
    WAKEminutes=$((EPGhours*60))                                                            # SET DEFAULT WAKE TIME
    if [[ "$(ps -A | grep "tvheadend")" != "" ]]; then                                                              # CHECK TVHEADEND ACTIVE
        wget --auth-no-challenge --no-check-certificate --http-user=$TVHnm --http-password=$TVHpwd -O - http://127.0.0.1:9981/status.xml > /dev/shm/statusTVH.txt    # GET STATUS
        subTVH=$(grep "subscriptions" /dev/shm/statusTVH.txt | awk -F '>' '{print $2}' | awk -F '<' '{print $1}')   # GET ACTIVITY STATUS
        WAKEminutes=$((WAKEminutes*(1-subTVH)))                                                                     # SET WAKE TIME TO ZERO WHILE SUBSCRIPTIONS ACTIVE
        nextTVH=$(grep "next" /dev/shm/statusTVH.txt | awk -F '>' '{print $2}' | awk -F '<' '{print $1}')           # GET MINUTES TO NEXT PLANNED RECORDING
        if [ $WAKEminutes -gt ${nextTVH:-"$WAKEminutes"} ]; then WAKEminutes=$nextTVH; fi                           # SET WAKE TIME TO CLOSEST EVENT
    fi
    if [ $MARGINshutdown -gt $WAKEminutes ]; then continue; fi                              # TEST 5 WAKE TIME WITHIN SAFE MARGIN SHUTDOWN
    break                                                                                   # IF ALL TESTS FAIL FINISH THE LOOP FOR SHUTDOWN
done
sudo rtcwake -t $(($(date +%s)+(WAKEminutes-MARGINstartup)*60)) -m no                       # CALCULATE AND SET NEXT RTC WAKE UP
sudo shutdown -P now                                                                        # SHUTDOWN

Replies (4)

RE: Powersave, Auto-shutdown, Auto-startup - Added by Volitt n/a about 23 hours ago

Hello.

Thank you for the script. Unfortuantely, it doesn't work for me. The headless PC with tvheadend never shuts down. Is there way, to find out which test is causing this?

RE: Powersave, Auto-shutdown, Auto-startup - Added by Dutchy Nl about 19 hours ago

Change the first line of the script in next two lines:

#!/bin/bash -xv
exec 2>DUMP.txt

(Re)Start the script and wait long enough for the PC to shutdown.
As in your case expected the machine stays awake. Then check the file DUMP.txt to see the results of all commands in the script.

RE: Powersave, Auto-shutdown, Auto-startup - Added by Volitt n/a about 2 hours ago

The problem seems to be test 2. I changed NRtests=30 to NRtests=3 to speed everything up for testing. In DUMP.txt it always says:
+ '[' 0 -le 3 ']'
So, I think ${#netINarray[*]} isn't working correct. Unfortunately, me bash skill are not good enough to understand how it should work.

RE: Powersave, Auto-shutdown, Auto-startup - Added by Dutchy Nl 4 minutes ago

Check if "eth0" is the correct (name of the) network interface.
Open a terminal and type "ip link", "ifconfig" or "grep '' /proc/net/dev" to find out.

    (1-4/4)