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
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
Thanks Raj for the suggestion.
Thanks for the script, got it running on my centos 5 server.
Now back to coding 🙂
if [ ! -r “$pidfile” ] && [ $pidcount -ge 2 ];
This line (40) contains a typo, should have just the && not the HTML amp;amp; bits.
Thanks for noticing the issue. I have a link to download the script as plain text in the blog post itself.
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
Don,
I had the same experience. I just changed line 39 in obtainpid() to:
pidcount=`awk -v name=”$pidstr” ‘BEGIN{print split(name,a,” “);}’`
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.
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.
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.
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
Like others, I got “awk: fatal: attempt to use array `a’ in a scalar context”. According to http://objectmix.com/awk/361598-gawk-length-array-question.html , it is a bug in awk that has now been fixed. A work around is to replace “length” by “asort”, which also returns the length of an array.
Thanks for pointing out. 🙂 I’ll post a fixed version.
Thanks for this script! I found you via a google search, and the script works great on my CentOS5 server.
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
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
Thanks Mohanjith for the great post. This helped me out with my problem
I’d just like to say thanks. This is exactly what I was looking for.
thanks it helped a lot