#!/bin/sh # run-apache2 ## dgg mods of apachectl for start/stop flybase apache web server ## add start/stop of $HOME/etc/tomcat server (bin/tomcat.sh) ## jan04 - create specially tuned flybaseng-apache80.conf and flybaseng-apache8081.conf ## run two servers: port80 only for flybaseng, no other port listens (a high load delay) ## second server to handle ports 8081 (argos general), 81 (old fb), 7081 (old fb) 7084 (ng) ## and redirect all but 8081 to port80 server # CONFIGURATION -- BUT see below argos-env.sh ARGOS_ROOT=/bio/argos quiet=0 debug=0 #? make setpath=1 default? setpath=0 edconf=0 STATUS_URI="/server-status?auto" FULLSTATUS_URI="/server-status?notable" # -------------------- -------------------- # find_homedir - resolving softlink in $0 # assume real $0 is MY_HOME/bin/script_name # where MY_HOME/conf and other goodies are located - updated from ant/bin/ant find_MY_HOME() { PRG="$0" while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done MY_HOME=`dirname "$PRG"`/.. # make it fully qualified MY_HOME=`cd "$MY_HOME" && pwd` PRG=`basename $0` } get_tester() { # curl may be more standard than lynx now. tester=`which curl` if expr "$tester" : '.*no .*' > /dev/null; then tester=`which lynx` if expr "$tester" : '.*no .*' > /dev/null; then if [ -z "$DUMP_URL" ]; then DUMP_URL="curl"; fi if [ -z "$ALIVE_URL" ]; then ALIVE_URL="curl --head"; fi else if [ -z "$DUMP_URL" ]; then DUMP_URL="lynx -dump"; fi if [ -z "$ALIVE_URL" ]; then ALIVE_URL="lynx -dump -head"; fi fi else if [ -z "$DUMP_URL" ]; then DUMP_URL="curl"; fi if [ -z "$ALIVE_URL" ] ; then ALIVE_URL="curl --head"; fi fi } do_start() { echo "do_start $@" if [ $RUNNING -eq 1 ]; then echo "$0 $ARG: httpd (pid $PID) already running" return fi if $APACHE_HTTPD ; then ERROR=$? echo "$0 $ARG: httpd started" else echo "$0 $ARG: httpd could not be started" ERROR=3 fi } do_stop() { echo "do_stop $@" if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS" return fi if kill $PID ; then echo "$0 $ARG: httpd stopped (pid $PID)" else ## need to really kill it if it isn't responding to kill -TERM if kill -KILL $PID ; then echo "$0 $ARG: httpd killed (pid $PID)" else echo "$0 $ARG: httpd could not be killed (pid $PID)" ERROR=4 fi fi sleep 6 # dang; http from hell isn't dying ... make sure with the stake if kill -0 $PID 2>/dev/null ; then # may take a while to die if problems echo "$0 $ARG: httpd WONT STOP - needs a stake thru the heart (pid $PID)" ERROR=2 if kill -KILL $PID ; then echo "$0 $ARG: httpd killed (pid $PID)" else echo "$0 $ARG: httpd WONT DIE - could not be killed (pid $PID)" ERROR=4 fi fi } do_kill() { echo "do_kill $@" if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS" return fi if kill -KILL $PID ; then echo "$0 $ARG: httpd killed (pid $PID)" else echo "$0 $ARG: httpd could not be killed (pid $PID)" ERROR=4 fi } do_restart() { echo "do_restart $@" if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: httpd not running, trying to start" if $APACHE_HTTPD ; then ERROR=$? echo "$0 $ARG: httpd started" else echo "$0 $ARG: httpd could not be started" ERROR=5 fi else if $APACHE_HTTPD -t >/dev/null 2>&1; then if kill -HUP $PID ; then echo "$0 $ARG: httpd restarted (pid $PID)" else echo "$0 $ARG: httpd could not be restarted (pid $PID)" ERROR=6 fi else echo "$0 $ARG: configuration broken, ignoring restart" echo "$0 $ARG: (run 'apachectl configtest' for details)" ERROR=6 fi fi } do_graceful() { echo "do_graceful $@" if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: httpd not running, trying to start" if $APACHE_HTTPD ; then ERROR=$? echo "$0 $ARG: httpd started" else echo "$0 $ARG: httpd could not be started" ERROR=5 fi else if $APACHE_HTTPD -t >/dev/null 2>&1; then if kill -USR1 $PID ; then echo "$0 $ARG: httpd gracefully restarted (pid $PID)" else echo "$0 $ARG: httpd could not be restarted (pid $PID)" ERROR=7 fi else echo "$0 $ARG: configuration broken, ignoring restart" echo "$0 $ARG: (run 'apachectl configtest' for details)" ERROR=7 fi fi } ## urk; need also to edit various apache.conf include files ... ## need more complex script to handle this - perl check_edit_conf() { echo "check_edit_conf disabled" } # echo "check_edit_conf $@" # # echo "TESTING on-the-fly config settings from install.conf " # # ## ?? use apache.conf-dist for input ? and write to apache.conf ? # ## look for '@xxx@' in .conf # confed="${APACHE_CONF}-ed.local" # # ## this -nt not in solaris bin/sh -> bin/ksh # ## if test $confed -nt ${APACHE_CONF} ; then # ## if [ -r $confed ] ; then # tester=`which test` # if $tester $confed -nt ${APACHE_CONF} ; then # APACHE_CONF=$confed # echo "Using edited APACHE_CONF: $APACHE_CONF" # return # fi # # # want also to check file dates; only edit if old > new # ## fgrep '@' test is bad - got 'server@host.name' # ## fgrep '@' "${APACHE_CONF}" >/dev/null # egrep '@[A-z]*@' "${APACHE_CONF}" >/dev/null # if [ $? -eq 0 ]; then # echo "Rewrite @ENV_VAR@ in ${APACHE_CONF}" # cat ${APACHE_CONF} | perl -pe 's/\@([^@]*)\@/$ENV{$1}/g;' > $confed # APACHE_CONF=$confed # echo "Using edited APACHE_CONF: $APACHE_CONF" # fi ### from apache make # if [ ! -r "$TOP/$APACHE_CONF" ] ; then # (echo "##"; \ # echo "## ${APACHE_CONF} -- Apache HTTP server configuration file"; \ # echo "##"; \ # echo ""; \ # cat $(TOP)/conf/${APACHE_CONF}-dist ) |\ # sed \ # -e 's;@@ServerRoot@@/htdocs;$(htdocsdir);' \ # -e 's;@@ServerRoot@@/icons;$(iconsdir);' \ # -e 's;@@ServerRoot@@/cgi-bin;$(cgidir);' \ # -e 's;@@ServerRoot@@/proxy;$(proxycachedir);' \ # -e 's;@@ServerRoot@@;$(prefix);g' \ # -e 's;httpd\.conf;$(TARGET).conf;' \ # -e 's;logs/accept\.lock;$(runtimedir)/$(TARGET).lock;' \ # -e 's;logs/apache_runtime_status;$(runtimedir)/$(TARGET).scoreboard;' \ # -e 's;logs/httpd\.pid;$(runtimedir)/$(TARGET).pid;' \ # -e "s;logs/access_log;$(logfiledir)/$${target_prefix}access_log;" \ # -e "s;logs/error_log;$(logfiledir)/$${target_prefix}error_log;" \ # -e "s;logs/referer_log;$(logfiledir)/$${target_prefix}referer_log;" \ # -e "s;logs/agent_log;$(logfiledir)/$${target_prefix}agent_log;" \ # -e 's;conf/magic;$(sysconfdir)/magic;' \ # -e 's;conf/mime\.types;$(sysconfdir)/mime.types;' \ # -e 's;User nobody;User $(conf_user);' \ # -e 's;Group #-1;Group $(conf_group);' \ # -e 's;^Group "#-1";Group $(conf_group);' \ # -e 's;Port 80;Port $(conf_port);' \ # -e 's;ServerAdmin you@your.address;ServerAdmin $(conf_serveradmin);' \ # -e 's;ServerName new.host.name;ServerName $(conf_servername);' \ # > $(TOP)/$APACHE_CONF \ # else \ # echo "[PRESERVING EXISTING CONFIG FILE: $(root)$(sysconfdir)/$${target_conf}]"; \ # fi; \ set_apaths() { if [ ! -r "$APACHE_CONF" ] ; then echo "APACHE_CONF $APACHE_CONF is missing " exit 1 fi ## can we do this? - drop for now; leave to other management soft ## if [ $edconf -eq 1 ]; then check_edit_conf; fi if [ -z "$APACHE_HOME" ] ; then apid=`sed -ne 's/^ *ServerRoot *//p' $APACHE_CONF | sed -e's/"//g'` if [ "x$apid" != "x" ]; then APACHE_HOME=$apid; else APACHE_HOME="$ARGOS_ROOT/common/servers/apache"; fi export APACHE_HOME fi if [ ! -r "$APACHE_HOME/bin/httpd" ]; then echo "The APACHE_HOME environment variable is not defined correctly" echo "$APACHE_HOME/bin/httpd is missing " exit 1 fi # PATH="$ROOT/common/system-local/bin:$ROOT/common/bin:/bin:/usr/bin:/usr/local/bin:/usr/java/bin"; export PATH; apath="" syspath="/bin:/usr/bin:/usr/local/bin:/usr/java/bin"; # not ${PATH} if [ -z "$ARGOS_PATH" ]; then if [ ! -z "$ARGOS_ROOT" ]; then apath="$ARGOS_ROOT/common/system-local/bin:$ARGOS_ROOT/common/bin"; fi else apath=$ARGOS_PATH fi if [ ! -z "$apath" ]; then PATH="${apath}:${syspath}" ; export PATH; fi APACHE_HTTPD="$APACHE_HOME/bin/httpd -d $APACHE_HOME -f $APACHE_CONF" ## these are set in .conf ; may be overridden by included conf ? apid=`sed -ne 's/^ *PidFile *//p' $APACHE_CONF` if [ "x$apid" != "x" ]; then APACHE_PID=$apid; # should test for leading / if [ ! -f $APACHE_PID ] ; then APACHE_PID="$APACHE_HOME/$apid"; fi else APACHE_PID="$APACHE_HOME/logs/httpd.pid"; fi ## Port gone in ap2, only Listen apid=`sed -ne 's/^ *Listen *//p' $APACHE_CONF` if [ "x$apid" = "x" ]; then apid=`sed -ne 's/^ *Port *//p' $APACHE_CONF` fi if [ "x$apid" != "x" ]; then set $apid APACHE_PORT=$1 elif [ ! -z "$ARGOS_APACHE" ]; then APACHE_PORT=$ARGOS_APACHE else APACHE_PORT=80 fi case "$command" in "alive"|"status"|"fullstatus") echo "Using APACHE_PORT: $APACHE_PORT" ;; *) cat </dev/null ; then STATUS="httpd (pid $PID) running" RUNNING=1 else STATUS="httpd (pid $PID?) not running" RUNNING=0 fi else STATUS="httpd (no pid file) not running" RUNNING=0 PID=0 fi } # ----- Execute The Requested Command ----------------------------------------- do_command() { get_pid case "$command" in "graceful") do_graceful $catargs ;; "restart") do_restart $catargs ;; "start") do_start $catargs ;; "stop") do_stop $catargs ;; "kill") do_kill $catargs ;; "alive") if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS"; fi get_tester $ALIVE_URL "http://localhost:${APACHE_PORT}/" derr=$? if [ $derr -ne 0 ]; then ERROR=9 ; fi ;; "status") if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS"; fi get_tester $DUMP_URL "http://localhost:${APACHE_PORT}${STATUS_URI}" derr=$? if [ $derr -ne 0 ]; then ERROR=9 ; fi ;; "fullstatus") if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS"; fi get_tester $DUMP_URL "http://localhost:${APACHE_PORT}${FULLSTATUS_URI}" derr=$? if [ $derr -ne 0 ]; then ERROR=9 ; fi ;; "configtest") if [ $RUNNING -eq 0 ]; then echo "$0 $ARG: $STATUS"; fi if $APACHE_HTTPD -t; then : else ERROR=8 fi ;; "help"|*) echo "$0 $ARG: $STATUS"; get_tester cat <