summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/linhes-scripts/limit-mythcommflag.sh
blob: 8ca61c712aea2caab5fca2eba2d92f85d488820d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# limit-mythcommflag.sh   v0.1  05/17/09
# Utility to automatically limit mythcommflag if CPU usage is above a certain level.
# Uses cpulimit from http://cpulimit.sourceforge.net/
# Free for any use.
# Installation:
#  cp limit-mythcommflag.sh /usr/LH/bin
#  chmod +x /usr/LH/bin/limit-mythcommflag.sh
# Usage: Executed from runit limit-mythcommflag at boot

# Threshold for when mythcommflag will be paused
CPUTHRESHOLD=43

# Limit mythcommflag to percentage
LIMITCPU=20

# Log file to write (use /dev/null for no log)
#LOG=/var/log/mythtv/limit-mythcommflag.log
LOG=/dev/null

# Number of seconds to wait between checking for mythcommflag process
SLEEP=5

#sleep $SLEEP

touch $LOG

ALREADYLIMITED=0

while true; do
    PROCCOMMFLAG=`pidof mythcommflag`
    if [ -n "${PROCCOMMFLAG}" ]
    then
        FRONTENDCPU=`top -bn1u mythtv | grep mythfrontend | awk '{ print $9 }'`
        if [ "$FRONTENDCPU" -ge "$CPUTHRESHOLD" ]
        then
            echo "$(date) FE CPU $FRONTENDCPU% is greater than $CPUTHRESHOLD%, LIMIT Commflagging" # >> $LOG
            if [ $ALREADYLIMITED -eq 0 ]; then
                cpulimit -e mythcommflag -l $LIMITCPU &
		cpulimit_pid=$!
                ALREADYLIMITED=1
            fi
        else
            echo "$(date) FE CPU $FRONTENDCPU% is less than $CPUTHRESHOLD%, UNLIMIT Commflagging" #>> $LOG
            if [ $ALREADYLIMITED -eq 1 ]; then
	        kill $cpulimit_pid
                ALREADYLIMITED=0
            fi
        fi
#    else
#        echo "No COMMFLAG Process Active" >> $LOG
    fi
    sleep $SLEEP
done