In Bash I’m trying to get the process with most %CPU. Once i get it, i’m using awk to evaluate if the process must be killed or just change it’s nice. Once i get the process, here’s what i’m trying to do :
awk -v awkmax="$CPU_MAX" '
{
if( $3 > awkmax && $4 < 15 ) {
system("renice "$4"+5 -p "$1"")
}
else if ( $3 > awkmax && $4 == 15 ) {
system("kill -9 "$1"")
print "The process $1 has been killed.\n"
}
}'
where $3 is pcpu, $4 is the nice value and $1 is the pid.
My problem is here:
system("renice "$4"+5 -p "$1"")
it doesn’t work because of the "$4"+5, which is the actual nice of the process plus 5.
How can i pass that value to renice?
You have some quoting issues, the first
systemcall should be:And the second:
Space is the string concatenation operator in
awk.A note on
kill -9: it’s better to start with less severe signals before resorting to-9, as this doesn’t let the process do any cleaning up after it self, IIRC, start with SIGHUP (-1), then SIGINT (-2), then SIGTERM (-15), if none of these makes the process quit, then use SIGKILL (-9).