Annotation of badi/public_scripts/awstats_update_all/awstats_update_all, revision 1.2

1.1       adi         1: #!/bin/sh
                      2: 
                      3: 
                      4: # Update all awstats sites there is a config file for
                      5: 
                      6: 
                      7: AWSTATS_BIN="/usr/lib/cgi-bin/awstats.pl"
                      8: CONF_DIR="/etc/awstats"
                      9: CONF_TEMPLATE="awstats.default"
                     10: CONF_EXT="conf"
                     11: NICENESS=16
                     12: MAX_PROCESSES=8
                     13: # Wait that many seconds when MAX_PROCESSES are COUNT_PROC 
                     14: # to retry starting new update processes
                     15: RETRY=2
                     16: 
                     17: 
                     18: 
                     19: # ----don't edit below this line-----
                     20: 
                     21: # default to babbly, -q suppress output
                     22: SPEAK=true
                     23: 
                     24: if [ "$1" = "-q" ]; then
                     25:        SPEAK=false
                     26: fi
                     27: 
                     28: function count_proc() {
1.2     ! adi        29:        # count how many instances are already running
1.1       adi        30:        COUNT_PROC=$(ps -o command -u "$(id -u)" | egrep -cwe '^(/bin/sh|/usr/bin/perl)* *'"$AWSTATS_BIN")
                     31: }
                     32: 
                     33: function do_update() {
                     34:        # call awstats
                     35:        if $SPEAK; then 
                     36:                nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" &
                     37:        else
                     38:                # Print errors only
                     39:                nice -n "$NICENESS" "$AWSTATS_BIN" -update -config="$site" > /dev/null &
                     40:        fi
                     41: }
                     42: 
                     43: 
                     44: # call awstats for each configured domain
                     45: COUNT_PROC=0
                     46: CURRENT_DIR="$(pwd)"
                     47: cd "$CONF_DIR"
                     48: for conf in *.$CONF_EXT; do
                     49: 
                     50:         notstarted=true
                     51:         
                     52:        # control number uf update processes
                     53:         while [ $notstarted = true ]; do
                     54:                 count_proc
                     55:                 if [ $COUNT_PROC -lt $MAX_PROCESSES ]; then
                     56:                        # exclude the template file
                     57:                        if [ "$conf" != "$CONF_TEMPLATE" ]; then
                     58:                                site="$(echo "$conf" | sed -e "s/^awstats\.\(.*\)\.conf$/\1/")"
                     59:                                $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Starting update for $site (already running: $COUNT_PROC)."
                     60:                                do_update
                     61:                        fi
                     62:                         notstarted=false
                     63:                        let "site_count += 1"
                     64:                        break;
                     65:                fi
                     66: 
                     67:                sleep "$RETRY"
                     68:         done
                     69: 
                     70: done
                     71: cd "$CURRENT_DIR"
                     72: 
                     73: # wait for all processes to finish
                     74: count_proc
                     75: if [ $COUNT_PROC -gt 0 ]; then
                     76:         $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: All jobs started - $COUNT_PROC process(es) still runnning. Waiting..." 
                     77:         
                     78:         # wait for processes to finish
                     79:         while [ $COUNT_PROC -gt 0 ]; do
                     80:                 sleep "$RETRY"
                     81:                count_proc
                     82:         done
                     83: fi
                     84: 
                     85: $SPEAK && echo "`date "+%Y-%m-%d %H:%M:%S"`: Finished. Statistics for $site_count sites updated."
                     86: exit 0

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>