LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Blogs > catkin
User Name
Password

Notices

Rate this Entry

rsync server boot/control script

Posted 12-25-2010 at 12:14 AM by catkin

Hello

I couldn't find one to suit anywhere so wrote this.

It is not really needed during shutdown because all it does is send SIGTERM to all the rsync server processes and the Linux shutdown process does that anyway. rsync server processes are well behaved and do terminate quickly in response to SIGTERM so there's no need to check that the processes have terminated and escalate to SIGKILL (which the Linux shutdown process does anyway).

It is hardly worth using during boot because all it does is run /usr/bin/rsync --daemon --config=/etc/rsyncd.conf and that could easily be added to one of the boot supervisor scripts.

It is useful mostly for the status option and the comment in the get_pids function describing the rsync server process architecture which I could not find described on the 'net.

The stop option is useful when testing rsync's --partial option.

Purists please note it is a bash script and does use bashims.

Finally, after all those caveats, here it is:
Code:
#!/bin/bash

# 25dec10 Charles
#   * Creation

# Configure shell
# ~~~~~~~~~~~~~~~
export PATH=/usr/bin:/bin
set -o nounset
unalias -a
unset IFS   # Simple way to ensure bash works as if IFS is default

# Configuration variables
# ~~~~~~~~~~~~~~~~~~~~~~~
readonly PID_file=/var/run/rsyncd.pid
readonly start_command='/usr/bin/rsync --daemon --config=/etc/rsyncd.conf'

# Functions
# ~~~~~~~~~
function get_pids {
    # An rsync server has a master/daemon process that spawns a worker process
    # for each connection.
    # The master's PID is normally in the PID file and, being a daemon, it has a
    # PPID of 1.
    # The per-connection workers' PPIDs are the master's PID.
    # The per-connection worker processes may spawn further worker processes.
    # Further workers' PPID(s) are their per-connection workers' PID(s).
    # If the master is killed the per-connection worker processes become 
    # daemons.

    local array pid ppid

    # Use the PID file
    master_pid=
    if [[ -f $PID_file ]]; then
        master_pid=$( < $PID_file )
        if [[ $master_pid =~ ^[0-9]+$ ]]; then
            if ! ps -p $master_pid >/dev/null 2>&1 ; then
                echo "Removing stale $PID_file"
                rm -f "$PID_file"
                master_pid=
            fi
        else
            echo -e "Invalid $PID_file contents:\n$pid\nRemoving $PID_file" >&2
            rm -f "$PID_file"
            master_pid=
        fi
    fi
    # Use ps output
    worker_pids=
    while read line
    do
        if [[ $line =~ $start_command$ ]]; then
            array=( $line ); pid=${array[0]};  ppid=${array[1]}; 
            if [[ $ppid = 1 ]]; then
                if [[ $master_pid = '' ]]; then
                    master_pid=$pid
                else
                    [[ $pid != $master_pid ]] && echo "Another rsync daemon found, PID $pid"
                fi
            else
                worker_pids+=" $pid"
            fi
        fi
    done <<< "$( PS_PERSONALITY=linux ps -C rsync --no-headers -o pid,ppid,args )"
}

function start {
    get_pids
    if [[ $master_pid != '' ]]; then
        echo 'rsync daemon is already running'
        return
    fi
    echo "Starting rsync daemon ..."
    $start_command
}

function status {
    get_pids
    [[ $master_pid = '' ]] && msg_part='is not' || msg_part="(PID $master_pid) is"
    echo "rsync daemon $msg_part running"
    [[ ${#worker_pids} -gt 0 ]] && echo "rsync worker process(es):$worker_pids"
}

function stop {
    get_pids
    if [[ $master_pid = '' ]]; then
        echo 'rsync daemon is not running'
        return
    fi
    echo "Stopping rsync daemon and any worker processes ..."
    kill $master_pid $worker_pids
}

# Main sequence
# ~~~~~~~~~~~~~
case "${1:-}" in
    start )
        start
        ;;
    status )
        status
        ;;
    stop )
        stop
        ;;
    restart )
        stop
        sleep 1
        start
        ;;
    * )
        echo "Usage: ${0##*/} start|status|stop|restart" >&2
esac
Best

Charles
Posted in Uncategorized
Views 1013 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 10:29 AM.

Main Menu

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration