Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to implement the following pseudo-algorithm:

while true
do
  if there is 1G of free memory && server load below 60%
    ./some-script &
    break
  else
    sleep 10
  fi
done

I need help (advise, implementation notes on) with implementing:

  1. check on if there is 1G of free memory
  2. check on if the server load is less than 60%
share|improve this question
    
Your script could happen to never be run, because in Linux there is no free RAM shortly after system startup, it tries to use all for buffers and cache. See linuxatemyram.com - check the nice command how to start a command with low priority. And the renice command lets you adjust it later. –  ott-- Feb 18 '13 at 10:58
add comment

2 Answers

up vote 2 down vote accepted

You need to do a comparison against the free RAM, so we'll get that detail from /proc/meminfo.

Express load as a percentage by doing some sorcery (stolen outright from http://stackoverflow.com/a/9229692/1287561)

while true
do
  if [ `cat /proc/meminfo |grep MemFree|awk '{ print $2 }'` -lt 1048576 && `top -bn1 | grep "Cpu(s)" | \
       sed "s/.*, *\([0-9.]*\)%\id.*/\1/" | \
       awk '{print 100 - $1}'` -lt 60 ]
    ./some-script &
    break
  else
    sleep 10
  fi
done
share|improve this answer
2  
What shell are you using? < isn't a portable operator for numbers, use -lt instead. –  scai Feb 14 '13 at 7:30
    
@scai You're absolutely right, fixed. –  Joel E Salas Feb 14 '13 at 7:31
add comment

Just use batch(1), part of the at package. It runs the argument when load is low.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.