svnserve Init script

I was annoyed to have to start the svnserve as a daemon everytime I restarted the machine. I also wanted to use service configuration(GNOME) to deal with the service.

I looked all over the web and failed to find the a good one. So I thought of writing it my self. Last weekend I sat down and wrote the script. Here is the result.

This was tested on fc6 running kernel 2.6.20-1.2962.fc6. Subversion 1.4.2 (subversion-1.4.2-2.fc6). It should work in any distro with init.

To make service configuration aware of svn serve you will have to first copy the script to /etc/init.d and then run the following.

$ /sbin/chkconfig ---add svnserve

Also remember to create the configuration(/etc/sysconfig/subversion) file with the following lines in it to enable threading.

OPTIONS="--threads"

You can put any options you could send to svnserve in the configuration file.

PS: here is the Init script it self for your viewing before downloading

#!/bin/bash
#
# /etc/rc.d/init.d/subversion
#
# Starts the Subversion Daemon
#
# chkconfig: 2345 90 10
# description: Subversion Daemon
# processname: svnserve
# pidfile: /var/lock/subsys/svnserve

source /etc/rc.d/init.d/functions

[ -x /usr/bin/svnserve ] || exit 1

### Default variables
SYSCONFIG="/etc/sysconfig/subversion"

### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"

RETVAL=0
prog="svnserve"
desc="Subversion Daemon"
pidfile="/var/run/$prog.pid"

start() {
echo -n $"Starting $desc ($prog): "
daemon $prog -d $OPTIONS --pid-file $pidfile
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
touch /var/lock/subsys/$prog
fi
echo
}

obtainpid() {
pidstr=`pgrep $prog`
pidcount=`awk -v name="$pidstr" 'BEGIN{split(name,a," "); print length(a)}'`
if [ ! -r "$pidfile" ] && [ $pidcount -ge 2 ]; then
pid=`awk -v name="$pidstr" 'BEGIN{split(name,a," "); print a[1]}'`
echo $prog is already running and it was not started by the init script.
fi
}

stop() {
echo -n $"Shutting down $desc ($prog): "
if [ -r "$pidfile" ]; then
pid=`cat $pidfile`
kill -s 3 $pid
RETVAL=$?
else
RETVAL=1
fi
[ $RETVAL -eq 0 ] && success || failure
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$prog
rm -f $pidfile
fi
return $RETVAL
}

restart() {
stop
start
}

forcestop() {
echo -n $"Shutting down $desc ($prog): "

kill -s 3 $pid
RETVAL=$?
[ $RETVAL -eq 0 ] && success || failure
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$prog
rm -f $pidfile
fi

return $RETVAL
}

status() {
if [ -r "$pidfile" ]; then
pid=`cat $pidfile`
fi
if [ $pid ]; then
echo "$prog (pid $pid) is running..."
else
echo "$prog is stopped"
fi
}

obtainpid

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
RETVAL=$?
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status
;;
forcestop)
forcestop
;;
*)
echo $"Usage: $0 {start|stop|forcestop|restart|condrestart|status}"
RETVAL=1
esac

exit $RETVAL

If you enjoyed this post, make sure you subscribe to my RSS feed!

20 thoughts on “svnserve Init script”

  1. Thanks for the script, Mohanjith. Works good.

    One suggestion: the obtainpid did not get the pid on a RH Enterprise 5 machine for a svnserve that was not started with a pid-file option, and forcestop failed.

    I can investigate later why obtainpid failed, but you might want to add a check in stop and forcestop to make sure you have a valid pid before trying to kill it.

    -Raj

  2. if [ ! -r “$pidfile” ] && [ $pidcount -ge 2 ];

    This line (40) contains a typo, should have just the && not the HTML amp;amp; bits.

  3. Thanks for noticing the issue. I have a link to download the script as plain text in the blog post itself.

  4. Thanks for the script as our folks were looking for exactly this. We are using this on CentOS 4.5 and all went well until I used service to start svnserve. This is the result:

    [root@newmicros init.d]# service svnserve start
    awk: cmd. line:1: fatal: attempt to use array `a’ in a scalar context
    /etc/init.d/svnserve: line 40: [: -ge: unary operator expected
    Starting Subversion Daemon (svnserve): [ OK ]

    As you can see svn started and was verified with ps as running and having appropriate options. Any help?

    thanks
    don

  5. Don,

    I had the same experience. I just changed line 39 in obtainpid() to:

    pidcount=`awk -v name=”$pidstr” ‘BEGIN{print split(name,a,” “);}’`

  6. I tried to use this script and added the change that kevin suggested. But now I get “”invalid option: –pid-file” when I run start. I found that this is an option for daemon. But I cannot work out why it is a problem here. I’d like to start the process as the svn user too, not sure how to change the script to run the svnserve as svn user.

  7. Excellent post. Your instructions worked perfectly for me in Fedora 8. Minor nitpick though, the chkconfig command is actually:

    /sbin/chkconfig –add svnserve

    Also, please please please post your script ass a patch to the subversion package in Bugzilla so it’ll get added to the standard repo. I *hate* having to manually hack things just to get basic functionality, even if it is simple. There’s absolutely no reason why this init.d script shouldn’t be installed alongside svnserve.

  8. dave, I had the same problem – it was because an old copy of svnserve was in my /usr/bin directory. I deleted that and linked it to the one built in /usr/local/bin and the problem went away.

  9. Great, helped me on the spot! Thanks a ton Mohanjith’s. My system – CentOS 5.2, svnserve, version 1.4.2 (r22196) compiled Mar 14 2007, 20:55:55

  10. Thanks for this script! I found you via a google search, and the script works great on my CentOS5 server.

  11. Hi!

    Thanks for your great init-script. Unfortunately, it caused some problems on my debian server.
    ‘success’ and ‘failure’ don’t seem to exist.
    I also ran into the awk problem and replace this code with some bash code:

    obtainpid() {
    pidstr=`pgrep $prog`
    IFS=” ”
    pidarray=($pidstr)
    pidcount=${#pidarray}
    if [ ! -r “$pidfile” ] && [ $pidcount -ge 2 ]; then
    pid=${pidarray[0]}
    echo $prog is already running and it was not started by the init script.
    fi
    }

    Thanks again!

    Ciao,
    Andreas

  12. Works Well!

    Added a lockfile variable to be able to handle something like prog=”/opt/subversion/bin/svnserve-daemon” to go along with svn+ssh

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.