Sunday, October 3, 2010

quick start and stop functions for runit('s runsvdir)

I've been playing around with HomeBrew and I actually like it much more than I'd expected. I was in the process of migrating many services away from my pkgsrc and MacPorts setup and decided to write a couple of handy little shell functions to manage runit:


 1 #!/bin/ksh
 2 my_pathadd PATH ~/bin ~/scripts ~/.bin
 3 
 4 
 5 runit_start(){
 6     #handy little function for starting runit on a service directory speified
 7     #by ${SERVDIR}. 
 8     SERVDIR="/usr/local/var/service"
 9     RUNSVCMD="runsvdir -P ${SERVDIR}"
10 
11     if { ps -auxww | grep ${RUNSVCMD} | grep -v 'grep}then
12         echo 'already running'
13     else 
14         echo -n "starting runsvdir on ${SERVDIR}: "
15         #nohup runsvdir -P ${SERVDIR} &
16         eval nohup ${RUNSVCMD} &
17     fi
18 }
19 
20 
21 runit_stop(){
22     #function to stop runit
23     typeset i
24     for i in $(ps -auxww|grep ${RUNSVCMD}|grep -v 'grep'|awk '{print $2}')do
25         kill -1 ${i}
26     done
27 }
28 
29 runit_status(){
30     {ps -auxww|grep ${RUNSVCMD}|grep -v 'grep';} 2> /dev/null || echo "not running"
31 }


Updated to be portable across KSH and ZSH.... I don't care enough about BASH to check compatibility there (10-11-10):

SERVDIR="/usr/local/var/service"
RUNSVCMD="/usr/local/bin/runsvdir -P ${SERVDIR}"

runit_start(){
    #handy little function for starting runit on a service directory speified
    #by ${SERVDIR}. 
    if { ps -auxww | grep "${RUNSVCMD}" | grep -v 'grep' 2> /dev/null ;} ; then
        echo 'already running'
    else 
        echo -n "starting runsvdir on ${SERVDIR}: "
        #nohup runsvdir -P ${SERVDIR} &
        eval nohup " " ${RUNSVCMD} &
    fi
}


runit_stop(){
    #function to stop runit
    typeset i
    for i in $(ps -auxww|grep "${RUNSVCMD}"|grep -v 'grep'|awk '{print $2}'); do
        kill -1 ${i}
    done
}

runit_status(){
    { ps -auxww|grep "${RUNSVCMD}"|grep -v 'grep' 2> /dev/null ;} ||\
        echo "not running"
}