nmcaullay wrote:
Just incase anyone is interested, here is the babysit_commflag.sh that i implemented on my box (cut-n-paste from
the above post).
I ran with Nathan's version of the script a bit and came up with something that may be useful to the rest of you... First, I run it from rc.d instead of cron, so as not to fill up my system logs. Second, I made it check for new processes every 10 seconds to catch them faster. Here are the steps to install it (as root):
1. Create the script
/etc/init.d/babysit-commflag:
Code:
#! /bin/sh
### BEGIN INIT INFO
# Provides: babysit-commflag
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2345
# Default-Stop: 016
# Short-Description: Renice low priority tasks for better system performance
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/bin/babysit_commflag.sh
NAME="babysitter"
DESC="Babysitter"
SHELL=/bin/bash
test -x $DAEMON || exit 0
set -e
USER=mythtv
RUNDIR=/var/run/mythtv
ARGS=""
EXTRA_ARGS=""
NICE=0
ARGS="$ARGS $EXTRA_ARGS"
mkdir -p $RUNDIR
chown -R $USER $RUNDIR
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
if start-stop-daemon --quiet --stop --signal 0 --exec $SHELL $DAEMON
then
echo " already running."
exit
fi
start-stop-daemon --start --pidfile $RUNDIR/$NAME.pid \
--background --make-pidfile \
--chuid $USER --nicelevel $NICE --exec $SHELL $DAEMON -- $ARGS
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME "
start-stop-daemon --stop --oknodo --pidfile $RUNDIR/$NAME.pid \
--chuid $USER --exec $SHELL $DAEMON -- $ARGS
test -e $RUNDIR/$NAME.pid && rm $RUNDIR/$NAME.pid
echo "."
;;
restart|force-reload)
echo -n "Restarting $DESC: $NAME"
start-stop-daemon --stop --oknodo --pidfile $RUNDIR/$NAME.pid \
--chuid $USER --exec $SHELL $DAEMON -- $ARGS || : ignore errors
echo "."
sleep 3
start-stop-daemon --start --pidfile $RUNDIR/$NAME.pid \
--background --make-pidfile \
--chuid $USER --nicelevel $NICE --exec $SHELL $DAEMON -- $ARGS
echo "."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
NOTE: I run the script as the user mythtv, since all of the processes I want to renice are run as mythtv on my KnoppMyth R5.5 box. If this is not the case for you, change
USER=mythtv to
USER=root in the script.
2. Make the script executable:
Code:
chmod 0755 /etc/init.d/babysit-commflag
3. Install the rc.d script:
Code:
update-rc.d babysit-commflag defaults 91
4. Create the script
/usr/local/bin/babysit_commflag.sh:
Code:
#!/bin/bash
# Version: 26 November 2008
#
# Installation: ALL AS ROOT
# cp babysit_commflag.sh /usr/local/bin
# chmod +x /usr/local/bin/babysit_commflag.sh
#
# Usage: Executed from /etc/init.d/babysit-commflag at boot
# Processes to renice
# NOTE: mythfilldatabase is truncated in ps -Al output on my R5.5 system
LIST=( mencoder ffmpeg mythtranscode mythcommflag vamps mythfilldatabase mythfilldatabas )
# Log file to write (change to /dev/null for no log)
LOG=/var/log/mythtv/babysit_commflag.log
# Number of seconds to wait between checking for processes to renice
SLEEP=10
touch $LOG
while true; do
ps -Al|while read PROCESS; do
PRIORITY=`echo $PROCESS | awk '{ print $8 }'`
NAME=`echo $PROCESS | awk '{ print $14 }'`
PID=`echo $PROCESS | awk '{ print $4 }'`
listnum=${#LIST}
for ((i=0;i<$listnum;i++));do
if [ "$NAME" = "${LIST[${i}]}" ]; then
if [ "$PRIORITY" -lt "19" ]; then
echo -n "$NAME: " >> $LOG
renice 19 -p $PID >> $LOG
fi
break
fi
done
done
sleep $SLEEP
done
5. Make the script executable:
Code:
chmod 0755 /usr/local/bin/babysit_commflag.sh
6. Run it the first time:
Code:
/etc/rc5.d/S91babysit-commflag start
Whenever it renices a process, it will write a line to
/var/log/mythtv/babysit_commflag.log that looks like this:
Code:
mythcommflag: 15728: old priority 17, new priority 19
If you don't want a log, just change
LOG=/var/log/mythtv/babysit_commflag.log to
LOG=/dev/null in the script.
Enjoy,
Chael