Skip to content

Instantly share code, notes, and snippets.

@joseche
Created July 11, 2022 05:33
Show Gist options
  • Select an option

  • Save joseche/019faf5f4c9b06cddff20fc20153d9ad to your computer and use it in GitHub Desktop.

Select an option

Save joseche/019faf5f4c9b06cddff20fc20153d9ad to your computer and use it in GitHub Desktop.
Shell to run something when CPU IDLE > 99
#!/usr/bin/env bash
# check systems idle time, run commands depending on that
function get_idle {
idle=$(top -bn2 | grep "Cpu(s)"|tail -n 1|sed "s/.*, *\([0-9.]*\)%* id.*/\1/")
echo "$idle"
}
function main_loop {
while true; do
idle="$(get_idle)"
if [[ $idle > 99 ]]
then
echo "Do stuff now: $idle"
else
echo "Stop doing stuff now: $idle"
fi
sleep 30
done
}
main_loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment