#!/bin/bash set -e export BASEDIR=/home/groups/dehs export WORKDIR=$BASEDIR/dehs_prj/dehs PATH="$BASEDIR/bin:$PATH" LOCK=$BASEDIR/run.lock BNAME=$(basename "$0") attempts=0 maxattempts=30 while true; do if ! ln -s $$ $LOCK &>/dev/null; then PID=$(basename $(readlink -f $LOCK)) if [ ! -n "$PID" ] || ! ps -p "$PID" | grep "$BNAME" &>/dev/null; then echo "Removing stale lock file" rm $LOCK else if [ $attempts -eq $maxattempts ]; then echo "Max number of attempts ($maxattempts) to run reached, surrending to a forever running instance" >&2 exit 1 fi # lock file is okay, dehs.sh running # let's wait ten minutes and try again sleep $((10 * 60)) || exit 1 ((attempts++)) fi else break fi done run_type="-update_all" update_www=1 if [ ! -z "$1" ]; then case $1 in all) run_type="-update_all" ;; new) run_type="-update_new" ;; bogus) run_type="-update_bogus" ;; wwiz) run_type="-wwiz" ;; popcon) run_type="-dl_popcon -db_popcon" ;; bugs) run_type="-db_up_error" update_www=0 ;; db) run_type="-dehsqa_db" update_www=0 ;; www) run_type= ;; *) echo "Unknown run-type: $1" >&2 exit 1 ;; esac fi for rt in $run_type; do $WORKDIR/dehs_pg.php $rt >$WORKDIR/logs/stdout.log done c=0 if [ $update_www -gt 0 ]; then while ! $WORKDIR/update_wwwal.sh >/dev/null && [ $c -lt 50 ]; do sleep 5 ((c++)) done fi rm $LOCK