1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Script to shutdown program if load over X?

Discussion in 'Tomato Firmware' started by phdeez, Feb 26, 2010.

  1. phdeez

    phdeez Addicted to LI Member

    SOLVED: Script to shutdown program if load over X?

    UPDATE: Solved, see post 3 below.

    So, I've been trying to write a script to shutdown transmission-daemon if the CPU load is above 2 for the 5 minute average. Can someone take a look at my script and tell me what I'm doing wrong? I'm sure it's a variable or a semicolon somewhere.

    Code:
    #!/bin/sh
    
    #Script for monitoring 5min load average and killing transmission-daemon
    
    LOADAMT5=$(cat /proc/loadavg | cut -f 2 -d " ");
    LOADAMT1=$(cat /proc/loadavg | cut -f 1 -d " ");
    
    if [ "$LOADAMT5" -gt "2" ];
            then killall transmission-daemon;                                                      
            echo "Killed Transmision due to load" >> /var/log/messages;                            
            echo "5 minute load above 2.0: $LOADAMT5" >> /var/log/messages;        
            exit 0;            
    fi                         
                                                
    if [ "$LOADAMT1" -gt "3" ]; then                                           
                    killall transmission-daemon;                                   
                    echo "Killed Transmision due to load" >> /var/log/messages;    
                    echo "1 minute load above 3.0: $LOADAMT1" >> /var/log/messages;
            exit 0;                                                                
    fi
    I'm pretty sure the LOADAVG5/1 are working right:
    Code:
    root@520gu:/tmp/mnt/scripts/cpu# LOADAMT5=$(cat /proc/loadavg | cut -f 2 -d " ")
    root@520gu:/tmp/mnt/scripts/cpu# echo $LOADAMT5
    0.36
    root@520gu:/tmp/mnt/scripts/cpu# LOADAMT1=$(cat /proc/loadavg | cut -f 1 -d " ");
    root@520gu:/tmp/mnt/scripts/cpu# echo $LOADAMT1
    0.12
    root@520gu:/tmp/mnt/scripts/cpu# cat /proc/loadavg
    0.18 0.35 0.99 1/40 13164
    So, I'm pretty sure it's my 'if' statements, 'if' anyone has any insight :)

    I have crond running this script every 3 mins, and I'm expecting something to showup in my log, but alas, nothing happens, other than the script being run. (monitoring log via 'tail -f /var/log/messages' and top, so I know load is exceeding the -gt values after initiating a transfer via samba)
     
  2. mstombs

    mstombs Network Guru Member

    Use "logger ..." or "echo ... |logger" and not "echo ... >>var/log/messages" to get things in the system log.
     
  3. phdeez

    phdeez Addicted to LI Member

    Well, FWIW, I found out that bash scripts don't do well with floating point numbers (actually, at all). Instead, I'll use sed (or change the cut command to use periods instead) to parse the load from /proc/loadavg with whole numbers. I tried playing around with awk, but when the system load was high, it took too long to respond. I'll post an updated script soon.

    And... here it is- I changed the >> /var/log/messages to logger, reduced the number of cat /proc/loadavg called and changed the LOADAMT1/LOADAMT5 to integers by the use of the 'cut' command:
    Code:
    #!/bin/sh
    
    #Script for monitoring 1min/5min load average and killing transmission-daemon
    
    LOADAMT=$(cat /proc/loadavg);
    LOADAMT5=$(echo "$LOADAMT" | cut -f 2 -d " " | cut -f 1 -d ".");
    LOADAMT1=$(echo "$LOADAMT" | cut -f 1 -d " " | cut -f 1 -d ".");
    
    
    if [ "$LOADAMT5" -ge "3" -o "$LOADAMT1" -ge "4" ];
            then killall transmission-daemon;
            logger "Load too high- $LOADAMT";       
            logger "Killing Transmision due to load";
            exit 0;
    fi
     

Share This Page