Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.3: Control CPU usage while using fast user switching' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3: Control CPU usage while using fast user switching
Authored by: mlibbey on Jun 09, '07 03:34:59PM
I rewrote this a bit so that it works in 10.4 (well, at least the posted one didn't work for me)

#!/bin/sh
#
# switch_nice
#
# renice processes so that the user at the main login window gets priority

#optimization, only do the renice if the user has changed. Store that in /tmp
olduser=`cat /tmp/user`;

# Determine who is logged in at the main terminal
#
# The user currently logged on at the main main terminal will be returned
# as the final argument to the only instance of login running on std

x=`last -1| awk '{print $1}'`
TOPUSER=`ls -1 /Users/ | grep $x`

if (test $olduser != $TOPUSER); then
# the user has changed, so proceed
#change the user file so that olduser will get updated next time
echo "$TOPUSER" > /tmp/user;

# I like a bit of logging -- uncomment these if in the crontab you do 
#<path/to/switchnice >> /var/log
#date
#echo "new user, now $TOPUSER"

# Determine what other processes have been given priority by admins
# and ought to be exempt [the next should be 1 line]
EXEMPTIONS=(`ps -auxo nice | awk '{print $1, $2, $11}' | grep -v root | grep -v nobody | grep -e - | awk '{print $2}'`)

# using /Users for real users instead of trying to get it from a tool, since 
#there is less worry about system users and no truncation
# Get names of users to be reniced 
SWITCHEDUSER=(`ls -1 /Users/ | grep -Ev '(.DS|.local|Shared)'`)

#rest as in original
# Renice: others to 15, main terminal to 0, priority processes to -15
#echo  ${SWITCHEDUSER[*]}
echo ${SWITCHEDUSER[*]} | xargs renice 15 -u
renice 0 -u $TOPUSER
echo ${EXEMPTIONS[*]} | xargs renice -15 -p

# Add additional rules...I renice my seti@home instances to 20

fi



[ Reply to This | # ]