#!/bin/bash # chkconfig: 2345 98 2 # description: shellbox service for spawning programs . /etc/rc.d/init.d/functions export PATH=$PATH:/usr/local/bin export HOSTNAME=$(hostname -s) exe=/usr/local/bin/shellbox.tcl prog=shellbox params= conf=/etc/shellbox.conf confdir=/etc/shellbox.d logdir=/var/log/$prog shells=/var/run/$prog shopt -s nullglob [ -n "$logdir" -a -d $logdir ] || mkdir $logdir fail () { echo -n $@ failure echo exit 1 } launch () { if [ "$1" = "-reload" ] then reload=YES shift fi temp=$(mktemp -p $(dirname $shells)) || fail "can't create temporary file" cat $conf $confdir/[0-9]*.conf | \ while read PORT USER DIR COMMAND do # check for empty lines and comments [[ $PORT == "" || $PORT == \#* ]] && continue # check if already started shell is still alive if LINE=$(grep "$PORT $USER $DIR $COMMAND" $shells 2> /dev/null) then PID=${LINE%% *} if checkpid $PID then if [ -z "$reload" ] && [ -z "$*" ] || echo "$*" | grep -qw $PORT then echo_warning echo "Already running: $PORT $USER $DIR $COMMAND" fi echo "$LINE" >> $temp continue fi fi # check if we have to start all shells or only this PORT [ "$*" ] && echo "$*" | grep -qwv $PORT && continue if [ -n "$logdir" ] then LOG=$logdir/$PORT rm -f $LOG else LOG=/dev/null fi # start shellbox as other user echo -n Starting: $PORT $USER $DIR $COMMAND if sudo -E true 2> /dev/null then sudo -H -u $USER SHELLBOX=$HOSTNAME:$PORT $exe -dir $DIR $params $PORT $COMMAND >> $LOG 2>&1 < /dev/null & else export SHELLBOX=$HOSTNAME:$PORT sudo -H -u $USER $exe -dir $DIR $params $PORT $COMMAND >> $LOG 2>&1 < /dev/null & fi PID=$! # check if starting worked or failed sleep 0.1 if checkpid $PID then echo "$PID $PORT $USER $DIR $COMMAND" >> $temp echo else echo_failure echo cat $LOG fi done mv $temp $shells chmod 0644 $shells } start () { [ -r $conf ] || fail "$conf not readable" [ -x $exe ] || fail "$exe is not executable" launch $* touch /var/lock/subsys/$prog } stopshell() { PID=$1 PORT=$2 shift echo -n Stopping: $* kill $PID 2> /dev/null || echo_failure echo if [ $logdir ] then echo -e "\n**** stopped ****" >> $logdir/$PORT fi } stop () { # anything to stop? if [ ! -r $shells ] then echo -n "$prog: No shells started." success echo exit 0 fi if [ -z "$1" ] then # kill all shellboxes while read PID PORT ARGS do stopshell $PID $PORT $ARGS done < $shells rm -f $shells rm -f /var/lock/subsys/$prog else # kill only selected shellboxes temp=$(mktemp -p $(dirname $shells)) || fail "can't create temporary file" while read PID PORT ARGS do echo "$*" | grep -qw $PORT && stopshell $PID $PORT $ARGS || echo "$PID $PORT $ARGS" >> $temp done < $shells mv $temp $shells chmod 0644 $shells fi } reload () { echo "Reloading $conf: " [ -r $conf ] || fail "not readable" # anything to stop? if [ -r $shells ] then #first kill all shells that are not configured any more temp=$(mktemp -p $(dirname $shells)) || fail "can't create temporary file" while read PID ARGS do cat $conf $confdir/[0-9]*.conf | \ while read PORT USER DIR COMMAND do if [ "$PORT $USER $DIR $COMMAND" = "$ARGS" ] then echo "Keeping: $ARGS" echo "$PID $ARGS" >> $temp continue 2 fi done stopshell $PID $PORT $ARGS done < $shells mv $temp $shells chmod 0644 $shells fi #now start all new shells launch -reload } status () { [ -r $conf ] || fail "$conf not readable" if [ "$1" = "-log" ] then log=YES shift fi echo -e "pid\tport\tuser\tdir\t\t\tcommand" cat $conf $confdir/[0-9]*.conf | \ while read PORT USER DIR CMD do # check for empty lines and comments [[ $PORT == "" || $PORT == \#* ]] && continue # check if we have to report all shells [ "$*" ] && echo "$*" | grep -wqv $PORT && continue if [ "$logdir" -a "$log" ] then echo "-------------------------------------------------------------------" fi if LINE=$(grep "$PORT $USER $DIR $CMD" $shells 2> /dev/null) then PID=${LINE%% *} if checkpid $PID then echo -n $PID else $SETCOLOR_FAILURE echo -n DEAD $SETCOLOR_NORMAL fi else $SETCOLOR_FAILURE echo -n STOPPED $SETCOLOR_NORMAL fi echo -e "\t$PORT\t$USER\t$DIR\t$CMD" if [ "$logdir" -a "$log" ] then grep '\*\*\*\*' $logdir/$PORT 2>/dev/null fi done } CMD=$1 shift case "$CMD" in (start) start $*;; (stop) stop $*;; (restart) stop $*; start $*;; # kill all shells, then start again (reread|reload) reload $*;; # reload shellbox.conf without killing too much (status) status $*;; (*) echo "Usage: $0 {start [ports]|stop [ports]|restart [ports]|reload|status [-log] [ports]}" ;; esac