#!/bin/bash #. /etc/rc.status . /home/mix/rc.status.anon rc_reset LAUNCHER_PID="" APP_PID="" RELAUNCHES=0 function launcher() { if [ $# -lt 1 ]; then return 1; fi if ! [ -e $1 ]; then echo "Error: application'" $1 "' not found." return 1 fi if ! [ -x $1 ]; then echo "Error: '"$1"' is not executable." return 1 fi echo "launching application $1" RELAUNCHES=0 APP_PID="" export -n MIXPW=$MIXPW local LRET="$(echo $MIXPW | $1 $2)" if [ "$(echo $LRET | fgrep Could\ not\ decode\ mix\ certificate)" ]; then echo "Error: Could not decode mix certificate!" return 1 fi while [ "TRUE" ]; do APP_PID=$(ps auxwww | grep --regexp="$1" | grep -v grep | grep -v "$0" | awk '{ print $2 }') if [ -z "${APP_PID}" ]; then RELAUNCHES=$[$RELAUNCHES+1] echo "Relaunch #$RELAUNCHES of application $1" echo $MIXPW | $1 $2 fi sleep 2; done; } case "$1" in launch) shift 1 launcher $@ exit 0 ;; start) echo "Starting $PROGRAM_NAME..." cd $INSTALL_PATH ulimit -HSn 4096 if [ "$2" = "-p" ]; then echo -n "I need a passwd for the SignKey (if there is none just press ENTER):" read -s MIXPW export MIXPW=$MIXPW fi $LAUNCHER_COMMAND $EXECUTE_COMMAND $INSTALL_PATH/$PROGRAM $PROGRAM_OPTIONS & sleep 2 if [ ! "`$0 process`" ] then rc_failed 1 fi rc_status -v ;; stop) printf "Shutting down $PROGRAM_NAME..." LAUNCHER_PID=$(ps auxwww | grep --regexp="$EXECUTE_COMMAND" | grep --regexp="$LAUNCHER_COMMAND" | grep --regexp="$PROGRAM_NAME" | grep -v grep | awk '{ print $2 }') if [ "${LAUNCHER_PID}" ]; then kill -TERM "${LAUNCHER_PID}" >& /dev/null fi ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -TERM 2>/dev/null >/dev/null i=1 while [ $i -le 90 ] && [ "`ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep`" ]; do printf "." i=$[$i+1] if [ $i -eq 45 ]; then echo "" printf "Shutting down $PROGRAM_NAME, second try..." ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -TERM 2>/dev/null >/dev/null fi sleep 1 done if [ "`ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep`" ]; then ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -9 2>/dev/null >/dev/null rc_failed 6 fi true rc_status -v ;; reload) printf "Reloading $PROGRAM_NAME..." ps auxwww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep | awk '{ print $2 }' | xargs kill -HUP 2>/dev/null >/dev/null rc_status -v ;; restart) $0 stop $0 start ;; command) # executes the program with command line arguments if [ $# -lt 2 ] then $0 else shift 1 $EXECUTE_COMMAND $INSTALL_PATH/$PROGRAM $* while [ $# -gt 1 ] do shift 1 done fi ;; status) if [ ! "`$0 process`" ] then rc_failed 3 fi rc_status -v ;; process) ps auxww | grep --regexp="$PROGRAM_OPTIONS" | grep -v grep ;; log) cat $LOGFILE ;; version) $0 command $VERSION_OPTION | grep -i version ;; cvs) DO_UPDATE=0 if [ $# -gt 1 ] then if [ "$2" = "-update" ] then DO_UPDATE=1 shift 1 fi fi cd $INSTALL_PATH if [ ! -d cvs ] then DO_UPDATE=0 mkdir cvs fi cd cvs if [ -d $CVS_MODULE ] then if [ $DO_UPDATE -eq 0 ] then rm -R $CVS_MODULE fi else DO_UPDATE=0 fi if [ $DO_UPDATE -eq 0 ] then mkdir $CVS_MODULE cvs checkout $CVS_DATE $CVS_MODULE else cvs update $CVS_MODULE fi # delete the compiled program if it exists cd $CVS_MODULE if [ -e $PROGRAM ] then rm $PROGRAM fi rc_status -v ;; svn) cd $INSTALL_PATH if [ ! -d svn ] then mkdir svn fi cd svn if [ -d $SVN_MODULE ] then rm -Rf $SVN_MODULE fi svn export $SVN_MODULE_URL $SVN_MODULE rc_status -v ;; update) OLD_VERSION_OPTION=`$0 version` rc_reset NEW_VERSION_OPTION=`$EXECUTE_COMMAND $INSTALL_PATH/cvs/$CVS_MODULE/$PROGRAM $VERSION_OPTION | grep -i version` echo "OLD_VERSION_OPTION: $OLD_VERSION_OPTION" echo "NEW_VERSION_OPTION: $NEW_VERSION_OPTION" rc_status if [ "$?" -eq 0 ] then if [ "$NEW_VERSION_OPTION" \> "$OLD_VERSION_OPTION" ] then if [ ! -d $INSTALL_PATH/backup ] then mkdir $INSTALL_PATH/backup fi if [ -e $INSTALL_PATH/$PROGRAM ] then VERSION_COUNT=1 while [ -e "$INSTALL_PATH/backup/$PROGRAM~$VERSION_COUNT" ] do VERSION_COUNT=`expr $VERSION_COUNT + 1` done $0 stop rc_failed $? mv "$INSTALL_PATH/$PROGRAM" "$INSTALL_PATH/backup/$PROGRAM~$VERSION_COUNT" if [ -e $LOGFILE ] then mv "$LOGFILE" "$INSTALL_PATH/backup/$PROGRAM.log~$VERSION_COUNT" fi fi cp $INSTALL_PATH/cvs/$CVS_MODULE/$PROGRAM $INSTALL_PATH else rc_failed 5 fi fi rc_status case "$?" in 0) echo "Update complete! Please restart $PROGRAM_NAME ." ;; 5) echo "The current $PROGRAM_NAME is up-to-date. No update needed." ;; *) echo "Update of $PROGRAM_NAME failed!" ;; esac rc_status -v ;; compile) COMPILE_DIR="" if [ $# -gt 2 ] then if [ "$2" = "-d" ] then COMPILE_DIR="$3" shift 2 fi fi if [ ! "$COMPILE_DIR" ] then COMPILE_DIR="$INSTALL_PATH/cvs/$CVS_MODULE" fi echo "Compiling in directory '$COMPILE_DIR'!" if [ ! -d $COMPILE_DIR ] then rc_status 1 echo "Cannot compile - please specify a valid directory!" else echo "..compiling" cd $COMPILE_DIR if [ "$JAR_CLASSES" ] then # JAVA if [ -d classes ] then rm -R classes fi mkdir classes # go into the source directory cd src javac -d ../classes $COMPILE_OPTIONS $PROGRAM_NAME.java rc_failed $? if [ $? -ne 0 ] then rc_status -v rc_exit fi cd ../classes #rm -r META-INF/ JAR_EXEC="" for JAR in $JAR_CLASSES; do JAR_EXEC="$JAR_EXEC `jar -xf $JAR`" done rm -r META-INF/ # create the manifest file echo "Manifest-Version: 1.0" > ../MANIFEST.MF echo "Main-Class: $PROGRAM_NAME" | sed s/[/]/./g >> ../MANIFEST.MF jar cvfm $PROGRAM ../MANIFEST.MF * $JAR_EXEC rc_failed $? mv $PROGRAM .. cd .. else #C++ ./configure $COMPILE_OPTIONS rc_failed $? if [ $? -ne 0 ] then rc_status -v rc_exit fi make rc_failed $? fi fi rc_status -v ;; -p);; *) echo "" echo "This script controls an AN.ON service." echo "" echo "Usage: SERVICENAME {start|stop|restart|cvs|svn|compile [-d {directory}]|update|status|process|log|version|command {commands}}" echo "" echo "start|stop|restart: basic controls of the $PROGRAM_NAME" echo "cvs [-update]: fetches a new version from CVS; if -update is specified," echo " the code is only updated, not fully replaced" echo "svn : fetches a new version from SVN" echo "update: if the program version in the CVS directory is newer than the current version," echo " this makes a backup of the old version and replaces it; the service s stopped if update was successful;" echo "compile [-d {dir}]: compiles the program in the specified directory;" echo " if no directory is given, the program will be compiled in the default CVS directory" echo "status: checks if the $PROGRAM_NAME is running" echo "process: lists all $PROGRAM_NAME processes" echo "log: returns the $PROGRAM_NAME log" echo "version: returns the $PROGRAM_NAME version" echo "command {commands}: runs the program with the specified command line options" echo "" echo "Several arguments can be combined, for example if you want to update the service from CVS, try:" echo " SERVICENAME cvs compile update" echo "" exit 1 ;; esac # apply further commands if given if [ $# -gt 1 ] then shift 1 # the commands are only applied if everything was ok so far rc_status if [ $? -eq 0 ] then $0 $* fi fi rc_exit