User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive

I want to keep my media server on for as little time as possible while at the same time I don't want to manually turn it on every time I want to use it. Turning it on right before I reach home after work is no big deal most of the time, I can also easily turn it on with a wakeonlan call from my phone, turning it off however was more of a problem as I don't know how late I'm going to stay up to watch a film or TV series. I stream all the media from the server over DLNA to both my TV, my PC and my phone.

 The result was that I needed a script that could detect if media was streaming over the network, and if no streams were detected for about 20 minutes, suspend the server. I already have a bash script in place that suspends the server at certain times and also wakes it up, this is working ok but I wanted it to be a little more intelligent. This is still a work in prograss but this is what I got so far.

The problematic part here was that tcpdump just sits there if it doesn't get any packages so I needed to add something that kills it if there is no traffic. This is the result as of now:


# Check if any unit is streaming video, need to be root
LASTTIMEOFSTREAM='date -d$(/usr/sbin/tcpdump -i eth0 -c 1 -q src and port 8895 2> /dev/null | cut -d. -f1) +%s &'


sleep 2

if [ -d /proc/$PID ]
  kill -15 $PID 2> /dev/null

This script is small an can be called with 

RESULT=$(sudo check_if_streaming); if [ -z $RESULT ]; then echo "1"; else echo "0";fi

to get a 1 or a 0 depending on if there is any traffic going out on port 8895, it's asomewhat workaroundish way to get a proper retval from the script :). So now I can check from the suspend script if it's OK to suspend or if anyone is streaming from the server. This is the suspend script by the way:

# Auto suspend and wake-up script
# Puts the computer on standby and automatically wakes it up at specified time
# Written by Romke van der Meulen <This email address is being protected from spambots. You need JavaScript enabled to view it.>
# Minor mods fossfreedom for AskUbuntu
# Takes a 24hour time HH:MM as its argument
# Example:
# suspend_until 9:30
# suspend_until 18:45
# ------------------------------------------------------
# TODO: check if hw clock is UTC or local
# TODO: add dryrun option
# Set to 1 to always suspend no matter what, set to 0 to consider arguments
# Path to log: LOG=/var/log/suspend_until.log
# Don't toch
DESIRED=$((`date +%s -d "$1"`))
NOW=$((`date +%s`))
ONLINE=$(who | cut -f1 -d " " | uniq)
# File with users that disables suspend if logged on
# Devices that disables suspend if online, hostname or IP, comma separated
# Argument check
if [ $# -lt 1 ]; then
    echo "Usage: suspend_until HH:MM" >> $LOG
# Check whether specified time today or tomorrow
if [ $DESIRED -lt $NOW ]; then
    DESIRED=$((`date +%s -d "$1"` + 24*60*60))
# Get devices that will disable suspend
for device in $HDEVICE; do 
   if [[ $(/bin/ping -c 1 $device) && $2 != '-f' ]] 
echo "$(date '+%c') Honoured devices online, not suspending"  >> $LOG
# Kill rtcwake if already runnin
/usr/bin/killall rtcwake > /dev/null 2>&1
# See if honoured users are logged on
for users in $ONLINE; do
   if [[ $(grep $users $HONOUR) && $2 != '-f' ]]
   then echo "$(date '+%c') Specified users online, not suspending"  >> $LOG
exit 0

# Set RTC wakeup time
# N.B. change "mem" for the suspend option
# find this by "man rtcwake"

if [[ $WILLSUSP == 1 ]]
echo "$(date '+%c') Suspending..."  >> $LOG
echo "$(date '+%c') Time to wake: $1" >> $LOG
/usr/sbin/rtcwake -u -m mem -t $DESIRED &
exit 0
elif [[ $2 == '-f' ]]
echo $1
        echo "$(date '+%c') Force suspending..."  >> $LOG
        echo "$(date '+%c') Time to wake: $1" >> $LOG
        /usr/sbin/rtcwake -u -m mem -t $DESIRED &
exit 0


There is one more thing though, after waking up the server it won't automatically connect to the VPN tunnel so I did this:

/bin/ping -w 1 > /dev/null
if [[ $RETVAL_PING -eq 0 ]] 
        echo "Fine!"
echo "No connection, start VPN"
        nmcli con down id "VPN"
        sleep 3
        nmcli con up id "VPN"