#!/bin/bash # $Id$ if [ -z $GNUSTEP_SYSTEM_ROOT ]; then source $HOME/SkyrixRoot/Makefiles/GNUstep.sh echo "Using GNUstep: ${GNUSTEP_SYSTEM_ROOT}" fi DAEMON="skymasterd" LSOF='/usr/sbin/lsof' if [ ! -e $LSOF ] then LSOF='/usr/bin/lsof' if [ ! -e $LSOF ] then LSOF=`which lsof` if [ -z $LSOF ] then echo -n "No lsof found, neither in /usr/bin nor in /usr/sbin or " echo "your path." exit 1 fi fi fi setURL () { URL="" PORT=`$LSOF -Pi | grep skymaster | grep LISTEN | awk '{print $8}' -` if [ -z "$PORT" ] then return fi sub=`expr substr $PORT 1 9` if [ "x$sub" = "xlocalhost" ] then PORT=`echo $PORT | sed s/*:// -` URL="http://$PORT/RPC2" else URL="http://localhost:$PORT/RPC2" fi } PIDFILE="$GNUSTEP_USER_ROOT/run/$DAEMON.pid" PREFIX=`Defaults read NSGlobalDomain SxDefaultNamespacePrefix` NAMESPACE="$PREFIX.master" LOGDIR="$GNUSTEP_USER_ROOT/logs" LOGFILE="$LOGDIR/skymasterd.log" STARTUSER="skyrix42" CHECK=10 CHECKINTERVAL=1 startDaemon () { if [ ! -d $LOGDIR ] then if [ $USER == "root" ] then su - $STARTUSER "mkdir $LOGDIR" else sh -c "mkdir $LOGDIR" fi fi printf "%-50s" "Starting service $DAEMON" if [ -f $PIDFILE ] then PID=`cat $PIDFILE` PROCPATH="/proc/$PID/environ" if [ -e $PROCPATH ] then echo "$DAEMON (pid $PID) already running" exit fi fi if [ $USER == "root" ] then su - $STARTUSER "nohup $DAEMON 1>>$LOGFILE 2>&1 &" else sh -c "nohup $DAEMON 1>>$LOGFILE 2>&1 &" fi COUNTER=0 for (( ; $COUNTER < $CHECK ; COUNTER = $COUNTER + 1 )) ; do WAIT=0 for (( ; $WAIT < 5 ; WAIT = $WAIT + 1 )); do echo -n "." sleep 1 setURL if [ -n "$URL" ] then break echo 'breaking' fi done echo -n "." STATUS=`sh -c "xmlrpc_call $URL $NAMESPACE.status"` if [ $? -eq 0 ] then if [ "x$STATUS" = "xrunning" ] then echo "OK" exit fi ; fi sleep $CHECKINTERVAL done echo "FAILED" } stopDaemon () { printf "%-50s" "Stopping service $DAEMON" if [ -f $PIDFILE ] then PID=`cat $PIDFILE` /bin/kill -INT $PID COUNTER=0 for (( ; $COUNTER < $CHECK ; COUNTER = $COUNTER + 1 )) ; do echo -n "." setURL sh -c "xmlrpc_call $URL $NAMESPACE.templates 1>/dev/null 2>&1" if [ $? -eq 255 ] then echo "OK" break fi ; sleep $CHECKINTERVAL done if [ $CHECK == $COUNTER ] then echo "FAILED" fi else echo "$DAEMON (no pid file) not running" fi } reloadDaemon () { printf "%-50s" "Reloading $DAEMON config" if [ -f $PIDFILE ] then PID=`cat $PIDFILE` kill -HUP $PID if [ $? -eq 0 ] then echo "OK" else echo "FAILED" fi else echo "$DAEMON (no pid file) not running" fi } case $1 in 'start') startDaemon ;; 'stop') stopDaemon ;; 'restart') stopDaemon startDaemon ;; 'reload') reloadDaemon ;; 'status') if [ -f $PIDFILE ] then PID=`cat $PIDFILE` PROCPATH="/proc/$PID/environ" if [ -e $PROCPATH ] then printf "%-50s" "Daemon $DAEMON is" echo "running" else echo "$DAEMON (no process found) not running" exit fi else echo "$DAEMON (no pid) not running" exit fi sh -c "sxc_call $NAMESPACE tasks 1>/dev/null 2>&1" if [ $? -eq 255 ] then echo "no matching component found (can't connect to registry)" exit fi INSTANCES=`sxc_call $NAMESPACE instances 2>/dev/null` for instance in $INSTANCES do printf "%-50s" "Instance $instance is" STATUS=`sh -c "sxc_call $NAMESPACE isRunning $instance"` if [ $STATUS = 0 ] then echo "not running" else echo "running" fi done ;; *) echo echo "SKYRiX $DAEMON .rc script" echo echo "usage: $0 (start|stop|restart|reload|status|help)" echo echo "start - start $DAEMON" echo "stop - stop $DAEMON by sending SIGINT" echo "restart - restart $DAEMON if running, start if not running" echo "reload - reload $DAEMON config by sending SIGHUP" echo "status - show the status of the $DAEMON tasks" echo "help - this screen" ;; esac