Last active
September 25, 2020 21:19
-
-
Save kks32/429fc5c22bb10ee318a9a9cf0fbb6672 to your computer and use it in GitHub Desktop.
kill idle process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Name of process | |
process=standard | |
# Sleep time in seconds (interval to check) | |
sleepinterval=2 | |
# Threshold cpu use in % | |
cputhreshold=6.0 | |
# Max trials | |
maxtrials=3 | |
# Sleep for 60s before the first check for process is active or not | |
sleep 10 | |
# Ntrials | |
ntrials=0 | |
# Get process id | |
pid=$(ps -ef | grep $process | grep $USER | grep $JOB_ID | awk 'NR==2{print $2}') | |
# Check if a process is running | |
if [ -n "$pid" ]; then | |
# Infinite loop to check | |
while : | |
do | |
sleep $sleepinterval; | |
cpu=$(ps -p $pid -o %cpu,%mem,cmd | awk 'NR==2{print $1}') | |
cpucheck=$(echo "$cpu < $cputhreshold" |bc -l) | |
if [[ $cpucheck -eq 1 ]]; then | |
# This is where you should kill | |
# echo "CPU is low, we should probably kill" | |
ntrials=$((ntrials+1)) | |
ntrialscheck=$(echo "$ntrials > $maxtrials" | bc -l) | |
if [[ $ntrialscheck -eq 1 ]]; then | |
# echo "Ready to kill now" | |
kill -9 $pid | |
break | |
fi | |
else | |
# echo "CPU is HIGH" | |
ntrials=0 | |
fi | |
done | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sh ./kill-idle-process.sh & | |
standard & | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment