Project

General

Profile

Powersave, Auto-shutdown, Auto-startup

Added by Dutchy Nl about 4 years 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 [ ${#NETarray[*]} -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 -C $i --no-headers)" != "" ]]; then continue 2; fi                      # TEST 4 FOR ACTIVE PROCESSES AS SPECIFIED
    done
    WAKEminutes=$((EPGhours*60))                                                            # SET DEFAULT WAKE TIME
    if [[ "$(ps -C tvheadend --no-headers)" != "" ]]; 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 (1)

RE: Powersave, Auto-shutdown, Auto-startup - Added by saen acro about 4 years ago

4x350+pWh solar panels of roof can solve need of turning-off 50W PC forever.

    (1-1/1)