Skip to content

Instantly share code, notes, and snippets.

@zhaofeng-shu33
Last active October 19, 2019 17:30
Show Gist options
  • Save zhaofeng-shu33/1cf4ec5186a301ca0883360dcfef3e8d to your computer and use it in GitHub Desktop.
Save zhaofeng-shu33/1cf4ec5186a301ca0883360dcfef3e8d to your computer and use it in GitHub Desktop.
cpu limit cron job shell script
#!/bin/bash
# get the top 5 processes
result=1490.0
mapfile -t proc_list < <(ps --no-headers --sort=-pcpu -Ao pid,pcpu | head -n 5)
for i in "${proc_list[@]}"; do
process_cpu_list=($i)
process_id=${process_cpu_list[0]}
cpu_usage_percentage=${process_cpu_list[1]}
compare_result=$(awk 'BEGIN{ print '$cpu_usage_percentage'<'$result' }')
if [ ! "$compare_result" -eq 1 ]; then
nohup /usr/local/bin/cpulimit -b -p $process_id -l $result -z
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment