Below is some pseudo code to illustrate some options for the weekly
script.  It's not meant as a solution but as a starting point for
discussion to Ted Unangst's request regarding laptops and desktops and
Nick Holland's request that ill-considered babble be accompanied by code.

Using at to schedule the maintenance could help -- at jobs get run later
if the machine was off.  But a method, not quite provided here, is
needed to keep all the backlogged jobs from running the minute the
machine is turned on again.

/Lars



Index: weekly
===================================================================
RCS file: /cvs/src/etc/weekly,v
retrieving revision 1.23
diff -u -p -r1.23 weekly
--- weekly      25 May 2009 21:31:24 -0000      1.23
+++ weekly      30 Jan 2010 20:09:53 -0000
@@ -11,6 +11,15 @@ MAINOUT=/var/log/weekly.out
 install -o 0 -g 0 -m 600    /dev/null $PARTOUT
 install -o 0 -g 0 -m 600 -b /dev/null $MAINOUT

+THIS_SCRIPT=`readlink -f $0`
+
+# do we check system load before running?
+if [ ${2:-' '} = 'look' ] || [ ${1:-' '} = 'look' ]; then
+   TURKEYLOOK=1; # check system load
+else
+   TURKEYLOOK=0; #
+fi
+
 start_part() {
        TITLE=$1
        exec > $PARTOUT 2>&1
@@ -40,36 +49,97 @@ run_script() {
        . $f
 }

+turkeylook() {
+    # apm status 1 should mean plugged in to external charger

+    if [ 1 -eq `apm -a` ]; then
+        # this currently wastes time, a no-delay method is needed

+        # Column 19 of vmstat's output should be the cpu percentage
idle
+        if [ "80" -le "` vmstat -c 2 | awk 'END {print $19}'`" ];then
+            return 0; # plugged in and rather idle

+        else
+            return 2; # plugged but not idle

+        fi
+    fi
+    return 1; # not plugged

+}
+
+
 start_part "Running weekly.local:"
 run_script "weekly.local"

-next_part "Rebuilding locate database:"
-if [ -f /var/db/locate.database ]; then
-       TMP=`mktemp /var/db/locate.database.XXXXXXXXXX`
-       if [ $? -eq 0 ]; then
-               trap 'rm -f $TMP; exit 1' 0 1 15
-               UPDATEDB="/usr/libexec/locate.updatedb"
-               echo "${UPDATEDB} --fcodes=- --tmpdir=${TMPDIR:-/var/tmp}" | \
-                   nice -5 su -m nobody 2>/dev/null 1>$TMP
-               if [ -s "$TMP" ]; then
-                       chmod 444 $TMP
-                       chown root:wheel $TMP
-                       mv -f $TMP /var/db/locate.database
-               else
-                       echo "Not installing locate database; zero size"
-               fi
-       else
-               echo "Not rebuilding locate database; can't create temp file"
+##
+###
+
+weekly_locate() {
+   if [ ${TURKEYLOOK} -gt 0 ]; then
+       turkeylook;
+               if [ $? -gt 0 ]; then
+          echo "${THIS_SCRIPT} locate look | at now +1 hour";
+          exit;
        fi
-fi
+   fi

-next_part "Rebuilding whatis databases:"
-/usr/libexec/makewhatis
+   next_part "Rebuilding locate database:"

-next_part "Doing login accounting:"
-[ "X$LOGINACCOUNTING" = X1 ] && {
-       ac -p | sort -nr -k 2
+   if [ -f /var/db/locate.database ]; then
+      TMP=`mktemp /var/db/locate.database.XXXXXXXXXX`
+      if [ $? -eq 0 ]; then
+        trap 'rm -f $TMP; exit 1' 0 1 15
+        UPDATEDB="/usr/libexec/locate.updatedb"
+        echo "${UPDATEDB} --fcodes=- --tmpdir=${TMPDIR:-/var/tmp}" | \
+             nice -5 su -m nobody 2>/dev/null 1>$TMP
+        if [ -s "$TMP" ]; then
+           chmod 444 $TMP
+           chown root:wheel $TMP
+           mv -f $TMP /var/db/locate.database
+        else
+           echo "Not installing locate database; zero size"
+        fi
+      else
+       echo "Not rebuilding locate database; can't create temp file"
+      fi
+   fi
 }
+
+##
+###
+
+weekly_whatis() {
+   if [ ${TURKEYLOOK} -gt 0 ]; then
+       turkeylook;
+               if [ $? -gt 0 ]; then
+          echo "${THIS_SCRIPT} whatis look | at now +1 hour";
+          exit;
+       fi
+   fi
+
+   next_part "Rebuilding whatis databases:"
+   /usr/libexec/makewhatis
+
+   next_part "Doing login accounting:"
+   [ "X$LOGINACCOUNTING" = X1 ] && {
+      ac -p | sort -nr -k 2
+   }
+}
+
+##
+###
+
+case "$1" in
+
+     'whatis')
+        weekly_whatis;
+     ;;
+
+     'locate')
+         weekly_locate;
+     ;;
+
+     *)
+         weekly_locate;
+         weekly_whatis;
+     ;;
+esac

 end_part
 rm -f $PARTOUT

Reply via email to