rsync daemon init script

Rsync, the popular file synchronization tool, can be used as a daemon. It is quite easy to start the daemon version of it, just execute:
rsync --daemon --config=/path/to/rsyncd.conf

If you want to start it at boottime you have to create an init script as it is not delivered with the rpm (or at least not with redhat/centos/fedora rpms).
You can search for any existing init scripts, for example systemimager-server delivers such init script, and use that script or write a simple init script yourself, using an existing script as guideline.

In my setup I needed 2 daemons, one running on port 873 (the default), the other one running on port 874. This is off course perfectly feasible but had some problems with the init scripts.
I wanted to be able to start and stop each daemon separately. But with the simple init script I could start both separately but when i wanted to stop one of them they are both stopped.

Diving into the daemon and killproc functions, which are stored in /etc/rc.d/init.d/functions and used by all init scripts, showed me that I have to use a combination of storing the pid file and the base filename of the init script.
The pid of the rsync daemon can be stored in any file (this is handle by rsync itself), this can be configured in the rsyncd configuration with following line: pid file = /var/run/rsyncd.pid

Following init script can start or stop the specific rsync daemon even if there are multiple daemons running on the system.
There is only requirement for this init script, which is explicitly explained in the script, the filename of the initscript must be the basename of the pidfile. For example if your initscript is /etc/init.d/custon-rsyncd the pid must be stored in /var/run/custom-rsyncd.pid.

#! /bin/sh
#
# chkconfig:   2345 50 50
# description: The rsync daemon

# source function library
 . /etc/rc.d/init.d/functions

PROG='/usr/bin/rsync'
BASE=${0##*/}

# Adapt the --config parameter to point to your rsync daemon configuration
# The config file must contain following line:
#  pid file = /var/run/<filename>.pid
# Where <filename> is the filename of the init script (= this file)
OPTIONS="--daemon --config=/etc/rsyncd.conf"

case "$1" in
  start)
    echo -n $"Starting $BASE: "
    daemon --check $BASE $PROG $OPTIONS
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$BASE
    echo
    ;;
  stop)
    echo -n $"Shutting down $BASE: "
    killproc $BASE
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$BASE
    echo
    ;;
  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

One thought on “rsync daemon init script

  1. Doug Barry

    I think its worth pointing out that killproc is (on most systems) not installed as a seperate executable, but rather a part of the Bourne Again SHell. Therefore the top line should be:
    #! /bin/bash
    In order for the script to be able to kill the process. Additionally when copied and pasted, this article has backquotes around the …Shutting down… echo, which of course throw a wobbly when output, so be sure to change them to normal quotes (“).
    Other than that, this script quickly solved my problems, and chkconfig and rsync are now living happily ever after.

    Thanks,

Comments are closed.