#!/bin/sh # # Description: Control the crontab for a user # # Author: Philip Gwyn # Support: philip@awale.qc.ca # License: GNU General Public License (GPL) # Copyright: (C) 2007 Philp Gwyn / Awalé. # # usage: ./daemontool [] {start|stop|status|restart} # daemontool::[::] # # : the name of the service's sub-directory # : Time (in seconds) to let the process start # # # Default delay (in seconds) DELAY=10 # Directory that daemontools is installed in DTDIR="" HADIR=/etc/ha.d . $HADIR/shellfuncs if [ -d "$DTDIR/service" ] ; then true # Normaly it goes in / (put "") but I HATE that, so i use /var/daemontools elif [ -d "/var/daemontools/service" ] ; then DTDIR="/var/daemontools" else ha_log ERROR "Can't find daemontools service directory" exit 1 fi DTSERVICE="$DTDIR/service" SVC="$DTDIR/command/svc" SVSTAT="$DTDIR/command/svstat" ############################################################### #### Simple usage statement usage() { echo "Usage: $0 [] {start|stop|status|restart}" } ############################################################### #### Find out if the service is running is_running() { T=$($SVSTAT $SERVICEDIR) if [ $? -ne 0 ] ; then # failed return 0 fi echo $T | grep "$SERVICEDIR: up " 2>/dev/null >/dev/null if [ $? -ne 0 ] ; then # failed return 0 fi return 1 } ############################################################### #### Start a service start() { is_running if [ $? -eq 1 ] ; then ha_log "ERROR $SERVICE already running" return 0 fi touch $SERVICEDIR/down ha_log "INFO Starting $SERVICE" $SVC -o $SERVICEDIR if [ "${DELAY:-0}" -gt 0 ] ; then ha_log "INFO Giving it $DELAY seconds" sleep $DELAY fi is_running if [ ! $? ] ; then ha_log "ERROR $SERVICE startup failed!" fi } ############################################################### #### Stop a service stop() { is_running if [ $? -eq 0 ] ; then ha_log "ERROR $SERVICE already stopped" return 0 fi ha_log "INFO Stoping $SERVICE" $SVC -d $SERVICEDIR touch $SERVICEDIR/down } ############################################################### #### Status of a service status() { is_running if [ $? -eq 1 ] ; then echo "$SERVICE is running" else echo "$SERVICE is stopped" fi } ############################################################### #### Parse the params if [ "$#" -eq 2 ] ; then SERVICE=$1 CMD=$2 SERVICEDIR="$DTSERVICE/$SERVICE" elif [ "$#" -eq 3 ] ; then SERVICE=$1 DELAY=$2 CMD=$3 SERVICEDIR="$DTSERVICE/$SERVICE" else usage >&2 exit 1 fi if [ ! -d "$SERVICEDIR" -o ! -x "$SERVICEDIR/run" ] ; then ha_log "ERROR: $SERVICE doesn't look like a daemontools service" exit 3 fi ############################################################### #### Run the command case "$CMD" in start) start ;; stop) stop ;; restart|reset) is_running if [ $? -eq 1 ] ; then stop fi start ;; status) status ;; *) usage >&2 exit 1 ;; esac exit 0