-
Notifications
You must be signed in to change notification settings - Fork 0
/
reSetPriority.sh
68 lines (51 loc) · 1.92 KB
/
reSetPriority.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/sh
### $Id: reSetPriority.sh,v 1.2 2020/11/08 21:03:28 root Exp $
###
### Script to reprioritize processes using language which is more intuitive to non-tech users.
abandonRenice()
{
echo "\n\t To ensure stable system, no process may be re-prioritized to value lower than '-18'! \n\t Please specify new option value.\n Bye!\n" ; exit 1
}
usage()
{
echo "\n\t Invalid parameter used on command line. Only allowed: [ --raise {difference} | --lower {difference} | --set {absolute} ]\n Bye!\n" ; exit 1
}
pidList()
{
echo "\n\t Must specify process ID list with '--pid' option.\n Bye!\n" ; exit 1
}
BASE=`basename "$0" ".sh" `
TMP="/tmp/${BASE}.nice"
mode=2
while [ $# -gt 0 ]
do
case $1 in
--raise ) mode=1; doNice="-${2}" ; shift ; shift ;;
--lower ) mode=1; doNice="${2}" ; shift ; shift ;;
--set ) mode=2; doNice="${2}" ; shift ; shift ;;
--pid ) PIDs="$*" ; shift $# ; break ;;
* ) usage ;;
esac
done
if [ -z "${PIDs}" ] ; then pidList ; fi
echo "\n NOTE: All processes with nice value of '-20' (aka system processes, maximum 'not-nice' meaning highest priority) and\n all spawns of 'kworker/' or 'ksoftirqd/' are ignored by this script ..."
#ps -eo "%p %r %P %n %c" >${TMP}
ps -eo pid,ni,comm | awk '{ if( $2 != "-20" && index($3,"kworker/") != 1 && index($3,"ksoftirqd/") != 1 ){ print $0 } ; }' >${TMP}
niceNOW=`for PID in \`echo ${PIDs} \`
do
cat ${TMP} | awk -v thisProc="${PID}" '{ if( $1 == thisProc ){ print $2 }; }'
done | sort -nr | uniq | head -1 `
case ${mode} in
1 ) niceNEW=`expr ${niceNOW} + ${doNice} ` ;;
2) niceNEW=${doNice} ;;
esac
### This approach was not interpreted as expected: 'expr -18 > ${niceNEW} '
if [ "-18" -gt "${niceNEW}" ] ; then abandonRenice ; fi
echo "\n Process(es) set with 'nice' value of => ${niceNEW} \n"
eval renice -n ${niceNEW} ${PIDs} | awk '{ printf("\t %s\n", $0 ) ; }'
echo ""
eval ps -o pid,ppid,pgid,ni,args ${PIDs}
echo ""
exit 0
exit 0
exit 0