#!/bin/bash # renicename - Renices the job that matches the specified name. setglobal user = ''""; setglobal tty = ''""; setglobal showpid = '0'; setglobal niceval = '"+1'" # initialize while getopts "n:u:t:p" opt { match $opt { with n setglobal niceval = $OPTARG; with u if test ! -z $tty { echo "$0: error: -u and -t are mutually exclusive." > !2 exit 1 } setglobal user = $OPTARG with t if test ! -z $user { echo "$0: error: -u and -t are mutually exclusive." > !2 exit 1 } setglobal tty = $OPTARG with p setglobal showpid = '1'; with ? echo "Usage: $0 [-n niceval] [-u user|-t tty] [-p] pattern" > !2 echo "Default niceval change is \"$niceval\" (plus is lower" > !2 echo "priority, minus is higher, but only root can go below 0)" > !2 exit 1 } } shift $shExpr('$OPTIND - 1') # eat all the parsed arguments if test $Argc -eq 0 { echo "Usage: $0 [-n niceval] [-u user|-t tty] [-p] pattern" > !2 exit 1 } if test ! -z $tty { setglobal pid = $[ps cu -t $tty | awk "/ $1/ { print \\$2 }] } elif test ! -z $user { setglobal pid = $[ps cu -U $user | awk "/ $1/ { print \\$2 }] } else { setglobal pid = $[ps cu -U $(USER:-LOGNAME) | awk "/ $1/ { print \$2 }] } if test -z $pid { echo "$0: no processes match pattern $1" > !2 ; exit 1 } elif test ! -z $[echo $pid | grep ' ] { echo "$0: more than one process matches pattern $(1):" if test ! -z $tty { setglobal runme = ""ps cu -t $tty"" } elif test ! -z $user { setglobal runme = ""ps cu -U $user"" } else { setglobal runme = ""ps cu -U $(USER:-LOGNAME)"" } eval $runme | \ awk "/ $1/ { printf \" user %-8.8s pid %-6.6s job %s\n\", \ \$1,\$2,\$11 }" echo "Use -u user or -t tty to narrow down your selection criteria." } elif test $showpid -eq 1 { echo $pid } else { # ready to go: let's do it! /bin/echo -n "Renicing job \"" /bin/echo -n $[ps cp $pid | sed 's/ [ ]*/ /g' | tail -1 | cut -d' ' -f6-] echo "\" ($pid)" renice $niceval $pid } exit 0