initscript doesn't work on some distributions #169

Open
matcho opened this Issue Feb 13, 2013 · 1 comment

1 participant

@matcho

The initscript provided in /tools doesn't work in some distributions like CentOS 6 or Mageia. It seems that, in a general way, it doesn't work with non debian-based distros or something like that.

The fact is, the script references the /lib/init/vars.sh file, which is not provided by the "initscripts" package in such distros (whereas it is in Ubuntu for example).

This is quite annoying as the workaround is to make couchdb run c-l as an "os_daemon", but it generates some issues relative to PID file in /var/run , and so on...

Thanks,
Mat

@matcho

Allow me to propose a working init script for CentOS 6. It was inspired by couchdb's init script.
It may be verbose and heavy but hey, I'm no admin! :)
Adapt the paths to your configuration.

#!/bin/sh
# couchdb-lucene daemon

### BEGIN INIT INFO
# Provides:          couchdb-lucene
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: couchdb-lucene init script
# Description:       couchdb-lucene init script for CentOS 6 on 2013-02-20 - based on couchdb init script
### END INIT INFO

DAEMON_PATH="/opt/couchdb/couchdb-lucene/bin"

SCRIPT_OK=0
SCRIPT_ERROR=1

DESCRIPTION="couchdb to lucene integration service"
NAME=couchdb-lucene
SCRIPT_NAME=`basename $0`
COUCHDB_LUCENE=/opt/couchdb/couchdb-lucene/bin/run
RUN_DIR=/opt/couchdb/couchdb-lucene/var/run
LSB_LIBRARY=/lib/lsb/init-functions

COUCHDB_LUCENE_USER=couchdb-lucene
COUCHDB_LUCENE_STDOUT_FILE=/dev/null
COUCHDB_LUCENE_STDERR_FILE=/dev/null
COUCHDB_LUCENE_PID_FILE=/opt/couchdb/couchdb-lucene/var/run/pid

if test ! -x $COUCHDB_LUCENE; then
    exit $SCRIPT_ERROR
fi

log_daemon_msg () {
    # Dummy function to be replaced by LSB library.

    echo $@
}

log_end_msg () {
    # Dummy function to be replaced by LSB library.

    if test "$1" != "0"; then
      echo "Error with $DESCRIPTION: $NAME"
    fi
    return $1
}

if test -r $LSB_LIBRARY; then
    . $LSB_LIBRARY
fi

run_command () {
    command="$1"
    if test -n "$COUCHDB_LUCENE_USER"; then
        if su $COUCHDB_LUCENE_USER -c "$command"; then
            return $SCRIPT_OK
        else
            return $SCRIPT_ERROR
        fi
    else
        if $command; then
            return $SCRIPT_OK
        else
            return $SCRIPT_ERROR
        fi
    fi
}

start_couchdb_lucene () {
    # Start couchdb-lucene as a background process.

    mkdir -p "$RUN_DIR"
    if test -n "$COUCHDB_LUCENE_USER"; then
        chown $COUCHDB_LUCENE_USER "$RUN_DIR"
    fi
    command="PIDFILE=$COUCHDB_LUCENE_PID_FILE $COUCHDB_LUCENE"
    if test -n "$COUCHDB_LUCENE_STDOUT_FILE"; then
        command="$command > $COUCHDB_LUCENE_STDOUT_FILE"
    fi
    if test -n "$COUCHDB_LUCENE_STDERR_FILE"; then
        command="$command 2> $COUCHDB_LUCENE_STDERR_FILE"
    fi
    if [ -f $COUCHDB_LUCENE_PID_FILE ]; then
        CURRENT_PID=`cat $COUCHDB_LUCENE_PID_FILE`
        log_daemon_msg "$NAME is already running with PID $CURRENT_PID"
    else
        run_command "$command"
    fi
}

stop_couchdb_lucene () {
    # Stop the running couchdb-lucene process.

    if [ -f $COUCHDB_LUCENE_PID_FILE ]; then
        CURRENT_PID=`cat $COUCHDB_LUCENE_PID_FILE`
        log_daemon_msg "trying to kill PID $CURRENT_PID"
        run_command "kill -9 $CURRENT_PID" > /dev/null
    else
        log_daemon_msg "$NAME is not running"
    fi
    run_command "rm $COUCHDB_LUCENE_PID_FILE" > /dev/null
}

display_status () {
    # Display the status of the running couchdb-lucene process.

    if [ -f $COUCHDB_LUCENE_PID_FILE ]; then
        CURRENT_PID=`cat $COUCHDB_LUCENE_PID_FILE`
        log_daemon_msg "$NAME is running with PID $CURRENT_PID"
    else
        log_daemon_msg "$NAME is not running"
    fi
}

parse_script_option_list () {
    # Parse arguments passed to the script and take appropriate action.

    case "$1" in
        start)
            log_daemon_msg "Starting $DESCRIPTION" $NAME
            if start_couchdb_lucene; then
                log_end_msg $SCRIPT_OK
            else
                log_end_msg $SCRIPT_ERROR
            fi
            ;;
        stop)
            log_daemon_msg "Stopping $DESCRIPTION" $NAME
            if stop_couchdb_lucene; then
                log_end_msg $SCRIPT_OK
            else
                log_end_msg $SCRIPT_ERROR
            fi
            ;;
        restart)
            log_daemon_msg "Restarting $DESCRIPTION" $NAME
            if stop_couchdb_lucene; then
                if start_couchdb_lucene; then
                    log_end_msg $SCRIPT_OK
                else
                    log_end_msg $SCRIPT_ERROR
                fi
            else
                log_end_msg $SCRIPT_ERROR
            fi
            ;;
        status)
            display_status
            ;;
        *)
            cat << EOF >&2
Usage: $SCRIPT_NAME {start|stop|restart|status}
EOF
            exit $SCRIPT_ERROR
            ;;
    esac
}

parse_script_option_list $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment