#!/bin/sh # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # Strictness to avoid folly set -e set -u prefix=@prefix@ exec_prefix=@exec_prefix@ OPT_ACTION="" OPT_CONFIG="" OPT_PID_DIR="" OSRF_HOSTNAME="" # --------------------------------------------------------------------------- # Make sure we're running as the correct user # --------------------------------------------------------------------------- [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit; usage() { cat << EOF usage: $0 [OPTION]... -c -a Mandatory parameters: -a action to perform Optional parameters:"; -c full path to C configuration file (opensrf_core.xml) -d store PID files in this directory -l accept 'localhost' as the fully-qualified domain name Actions include: start_router stop_router restart_router start_perl stop_perl restart_perl start_python stop_python restart_python start_c stop_c restart_c start_osrf stop_osrf restart_osrf stop_all start_all restart_all smart_clear - Clear all PID files that don't refer to a process clear_pid - Clear all PID files Examples: $0 -a restart_all $0 -l -c opensrf_core.xml -a restart_all EOF } # --------------------------------------------------------------------------- # Load the command line options and set the global vars # --------------------------------------------------------------------------- while getopts "a:d:c:lh" flag; do case $flag in "a") OPT_ACTION="$OPTARG";; "c") OPT_CONFIG="$OPTARG";; "d") OPT_PID_DIR="$OPTARG";; "l") export OSRF_HOSTNAME="localhost";; "h"|*) usage;; esac; done OSRF_CONFIG="@bindir@/osrf_config" [ ! -f "$OSRF_CONFIG" ] && OSRF_CONFIG=`which osrf_config` [ -z "$OPT_CONFIG" ] && OPT_CONFIG=`$OSRF_CONFIG --sysconfdir`/opensrf_core.xml; if [ ! -r "$OPT_CONFIG" ]; then echo "Please specify the location of the opensrf_core.xml file using the -c flag"; exit 1; fi; [ -z "$OPT_PID_DIR" ] && OPT_PID_DIR=`$OSRF_CONFIG --localstatedir`/run/opensrf; [ -z "$OPT_ACTION" ] && usage; PID_ROUTER="$OPT_PID_DIR/router.pid"; PID_OSRF_PERL="$OPT_PID_DIR/osrf_perl.pid"; PID_OSRF_PYTHON="$OPT_PID_DIR/osrf_python.pid"; PID_OSRF_C="$OPT_PID_DIR/osrf_c.pid"; # --------------------------------------------------------------------------- # Utility code for checking the PID files # --------------------------------------------------------------------------- do_action() { action="$1"; pidfile="$2"; item="$3"; if [ $action = "start" ]; then if [ -e $pidfile ]; then pid=$(cat $pidfile); reported=$(ps ax | grep "$item" | grep -v grep | awk '{print $1}' | awk '{ printf "%s ", $0 }') if [ "$pid " = "$reported" ]; then echo "$item already started : $pid"; return 0; else echo "$item not started, but PID file exists, removing file and starting"; rm $pidfile; return 0; fi; fi; echo "Starting $item"; fi; if [ $action = "stop" ]; then if [ ! -e $pidfile ]; then echo "$item not running"; return 0; fi; while read pid; do if [ -n "$pid" ]; then echo "Stopping $item process $pid..." kill -s INT $pid fi; done < $pidfile; rm -f $pidfile; fi; return 0; } # --------------------------------------------------------------------------- # Start / Stop functions # --------------------------------------------------------------------------- start_router() { do_action "start" $PID_ROUTER "OpenSRF Router"; opensrf_router $OPT_CONFIG routers sleep 2; # give the router procs time to fork and appear in ps pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}') echo $pid > $PID_ROUTER; return 0; } stop_router() { do_action "stop" $PID_ROUTER "OpenSRF Router"; return 0; } # The clean way to do this would be to invoke the "start_all" action # on opensrf.py, but that seems to randomly fail for services. So we # start each one separately from a bash script instead. start_python() { [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return; echo "Starting OpenSRF Python"; if [ -e $PID_OSRF_PYTHON ]; then #If python is started already (or it thinks so). cat << EOF Python processes are either already running, or were not correctly shut down. Now clearing any stale PID files and restarting Perl processes. EOF smart_clear; fi; OPT_LOCAL="" [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l" for service in `opensrf.py -a list_all $OPT_LOCAL`; do opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -d -a start -s $service $OPT_LOCAL [ "$?" = "0" ] && echo "Python Started" > $PID_OSRF_PYTHON; #Dummy pid file, removed when a proper shutdown happens. done return 0; } stop_python() { [ ! $($OSRF_CONFIG | grep OSRF_PYTHON) ] && return; echo "Stopping OpenSRF Python"; [ -e $PID_OSRF_PYTHON ] && rm $PID_OSRF_PYTHON; OPT_LOCAL="" [ "$OSRF_HOSTNAME" = "localhost" ] && OPT_LOCAL="-l" set +e # Ignore errors for NXDOMAIN opensrf.py -p $OPT_PID_DIR -f $OPT_CONFIG -a stop_all $OPT_LOCAL set -e # Errors matter again sleep 1; return 0; } start_perl() { echo "Starting OpenSRF Perl"; if [ -e $PID_OSRF_PERL ]; then #If perl is started already (or it thinks so) cat << EOF Perl processes are either already running, or were not correctly shut down. Now clearing any stale PID files and restarting Perl processes. EOF smart_clear; fi; opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR \ --config $OPT_CONFIG --action start_all --settings-startup-pause 3 [ "$?" = "0" ] && echo "Perl Started" > $PID_OSRF_PERL; #Dummy pid file, removed when a proper shutdown happens. return 0; } stop_perl() { echo "Stopping OpenSRF Perl"; opensrf-perl.pl --verbose --pid-dir $OPT_PID_DIR --config $OPT_CONFIG --action stop_all [ -e $PID_OSRF_PERL ] && rm $PID_OSRF_PERL; sleep 1; return 0; } start_c() { host=$OSRF_HOSTNAME if [ "_$host" = "_" ]; then host=$(perl -MNet::Domain=hostfqdn -e 'print hostfqdn()'); fi; do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)"; opensrf-c $host $OPT_CONFIG opensrf "$PID_OSRF_C"; return 0; } stop_c() { do_action "stop" $PID_OSRF_C "OpenSRF C"; [ -e $PID_OSRF_C ] && rm $PID_OSRF_C; sleep 1; return 0; } clear_pid() { echo "Clearing PID files..."; cd $OPT_PID_DIR; [ 0 -lt ls | wc -l ] && rm -v *.pid; return 0; } smart_clear() { echo "Smart clearing PID files..."; for line in $(find $OPT_PID_DIR -name *.pid -type f) do running="false"; for p in $(cat $line) do [ 0 -lt $(ps ax | grep "$p" | grep -v grep | wc -l) ] && running="true"; done if [ $running = "false" ]; then rm $line; echo "Removing stale PID file: $line"; fi; done return 0; } # --------------------------------------------------------------------------- # Do the requested action # --------------------------------------------------------------------------- case $OPT_ACTION in "start_router") start_router;; "stop_router") stop_router;; "restart_router") stop_router; start_router;; "start_perl") start_perl;; "stop_perl") stop_perl;; "restart_perl") stop_perl; start_perl;; "start_python") start_python;; "stop_python") stop_python;; "restart_python") stop_python; start_python;; "start_c") start_c;; "stop_c") stop_c;; "restart_c") stop_c; start_c;; "start_osrf") start_perl; start_c; start_python;; "stop_osrf") stop_python; stop_c; start_perl;; "restart_osrf") stop_python; stop_c; stop_perl; start_perl; start_c; start_python;; "stop_all") stop_python; stop_c; stop_perl; stop_router;; "start_all") start_router; start_perl; start_c; start_python;; "restart_all") stop_python; stop_c; stop_perl; stop_router; start_router; start_perl; start_c; start_python;; "clear_pid") clear_pid;; "smart_clear") smart_clear;; *) usage;; esac;