Skip to content

Instantly share code, notes, and snippets.

@jyalim
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyalim/e99fac4cf89afdfa2ecb to your computer and use it in GitHub Desktop.
Save jyalim/e99fac4cf89afdfa2ecb to your computer and use it in GitHub Desktop.
On remote Linux, track system load averaging and implement job with e-mail notification
#!/usr/bin/env bash
# NOTE ASSUMPTION OF LINUX
# NOTE ENVIRONMENT VARIABLES THAT MUST BE SET IN ADVANCE (OR REPLACED):
# export EMAIL='user@server.domain'
# export JOB='python path/to/src.py'
# export JOBNAME='python post processing'
# RUN THIS SCRIPT WITH NOHUP OR SCREEN OR TMUX
loadavg() {
cat /proc/loadavg | awk '{print (($1 + $2 + $3)<=3?1:0)}'
}
count=0
kount=0
# Increments $count when loadavg is below threshold, resets $count when
# loadavg is above threshold. If $count is below threshold for two
# consecutive periods, while loop ends and job begins. $kount tracks
# consecutive periods.
while true; do
res=$(loadavg)
[[ $res -eq 1 ]] && let count++ || count=0
sleep 600
[[ $count -gt 0 ]] && let kount++ || kount=0
[[ $kount -eq 2 ]] && break || :
done
mail -s 'Starting job' $EMAIL << __EOF
Starting $JOBNAME on $HOSTNAME.
__EOF
$JOB
mail -s 'Finished job' $EMAIL << __EOF
Finished $JOBNAME on $HOSTNAME.
__EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment