Source

iredapd / rc_scripts / iredapd.rhel

#!/usr/bin/env bash

# Author: Zhang Huangbin (zhb@iredmail.org)

### BEGIN INIT INFO
# chkconfig: - 99 99
# description: iredapd is a mail list access policy daemon.
# processname: iredapd
### END INIT INFO

PROG='iredapd'
BINPATH='/opt/iredapd/iredapd.py'
CONFIG='/opt/iredapd/etc/iredapd.ini'
PIDFILE='/var/run/iredapd.pid'

check_status() {
    # Usage: check_status pid_number
    PID="${1}"
    l=$(ps -p ${PID} | wc -l | awk '{print $1}')
    if [ X"$l" == X"2" ]; then
        echo "running"
    else
        echo "stopped"
    fi
}

start() {
    if [ -f ${PIDFILE} ]; then
        PID="$(cat ${PIDFILE})"
        s="$(check_status ${PID})"

        if [ X"$s" == X"running" ]; then
            echo "${PROG} is already running."
        else
            rm -f ${PIDFILE} >/dev/null 2>&1
            echo "Starting ${PROG} ..."
            python ${BINPATH} ${CONFIG}
        fi
    else
        echo "Starting ${PROG} ..."
        python ${BINPATH} ${CONFIG}
    fi
    unset s
}

stop() {
    if [ -f ${PIDFILE} ]; then
        PID="$(cat ${PIDFILE})"
        s="$(check_status ${PID})"

        if [ X"$s" == X"running" ]; then
            echo "Stopping ${PROG} ..."
            kill -9 ${PID}
            if [ X"$?" == X"0" ]; then
                rm -f ${PIDFILE} >/dev/null 2>&1
            else
                echo -e "\t\t[ FAILED ]"
            fi
        else
            echo "${PROG} is already stopped."
            rm -f ${PIDFILE} >/dev/null 2>&1
        fi
    else
        echo "${PROG} is already stopped."
    fi
    unset s
}

status() {
    if [ -f ${PIDFILE} ]; then
        PID="$(cat ${PIDFILE})"
        s="$(check_status ${PID})"

        if [ X"$s" == X"running" ]; then
            echo "${PROG} is running."
        else
            echo "${PROG} is stopped."
        fi
    else
        echo "${PROG} is stopped."
    fi
    unset s
}

case "$1" in
    start) start ;;
    stop) stop ;;
    status) status ;;
    restart) stop && sleep 1 && start ;;
    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
        ;;
esac
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.