Skip to content

Instantly share code, notes, and snippets.

@tknv
Forked from andsens/debian-init.sh
Created January 4, 2016 04:05
Show Gist options
  • Select an option

  • Save tknv/7029ab9a2a525dedca57 to your computer and use it in GitHub Desktop.

Select an option

Save tknv/7029ab9a2a525dedca57 to your computer and use it in GitHub Desktop.

Revisions

  1. @andsens andsens revised this gist Jun 3, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -13,11 +13,11 @@
    # Documentation available at
    # http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html
    # Debian provides some extra functions though
    source /lib/lsb/init-functions
    . /lib/lsb/init-functions


    DAEMON_NAME="generic-prog"
    DAEMON_USER="${DAEMON_NAME}"
    DAEMON_USER="generic-prog-user"
    DAEMON_PATH="/usr/local/bin/generic-prog"
    DAEMON_OPTS="-c /etc/generic-prog.conf"
    DAEMON_PWD="${PWD}"
    @@ -26,7 +26,7 @@ DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
    DAEMON_NICE=0
    DAEMON_LOG='/var/log/generic-prog'

    [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}"
    [ -r "/etc/default/${DAEMON_NAME}" ] && . "/etc/default/${DAEMON_NAME}"

    do_start() {
    local result
    @@ -107,4 +107,4 @@ stop) do_stop; exit $? ;;
    restart) do_restart; exit $? ;;
    status) do_status; exit $? ;;
    *) do_usage; exit 1 ;;
    esac
    esac
  2. @andsens andsens revised this gist Jun 3, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,7 @@ DAEMON_PWD="${PWD}"
    DAEMON_DESC=$(get_lsb_header_val $0 "Short-Description")
    DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
    DAEMON_NICE=0
    DAEMON_LOG='/var/log/generic-prog'

    [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}"

    @@ -36,6 +37,9 @@ do_start() {
    result=0
    else
    log_daemon_msg "Starting ${DAEMON_DESC}" "${DAEMON_NAME}"
    touch "${DAEMON_LOG}"
    chown $DAEMON_USER "${DAEMON_LOG}"
    chmod u+rw "${DAEMON_LOG}"
    if [ -z "${DAEMON_USER}" ]; then
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    @@ -103,4 +107,4 @@ stop) do_stop; exit $? ;;
    restart) do_restart; exit $? ;;
    status) do_status; exit $? ;;
    *) do_usage; exit 1 ;;
    esac
    esac
  3. @andsens andsens revised this gist Jun 1, 2014. 1 changed file with 41 additions and 26 deletions.
    67 changes: 41 additions & 26 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    #!/bin/bash -e
    #!/bin/sh
    ### BEGIN INIT INFO
    # Provides: generic-prog
    # Required-Start: $local_fs $remote_fs $network
    # Required-Stop: $local_fs $remote_fs $network
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Generic Prog
    # Description: Generic Prog is a generic program to do generic things with
    # Short-Description: Generic Program
    # Description: Generic Program is a generic program to do generic things with
    ### END INIT INFO


    # Documentation available at
    # http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html
    # Debian provides some extra function though
    # Debian provides some extra functions though
    source /lib/lsb/init-functions


    @@ -21,49 +21,64 @@ DAEMON_USER="${DAEMON_NAME}"
    DAEMON_PATH="/usr/local/bin/generic-prog"
    DAEMON_OPTS="-c /etc/generic-prog.conf"
    DAEMON_PWD="${PWD}"

    STARTUP_MSG="Starting generic-prog daemon"
    SHUTDOWN_MSG="Stopping generic-prog daemon"

    DAEMON_DESC=$(get_lsb_header_val $0 "Short-Description")
    DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
    DAEMON_NICE=0

    [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}"


    do_start() {
    local result
    log_daemon_msg "${STARTUP_MSG}" "${DAEMON_NAME}"
    if [ -z "${DAEMON_USER}" ]; then
    start_daemon -n $DAEMON_NICE -p "${DAEMON_PID}" "${DAEMON_PATH}" $DAEMON_OPTS
    result=$?

    pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
    if [ $? -eq 0 ]; then
    log_warning_msg "${DAEMON_NAME} is already started"
    result=0
    else
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    --chdir "${DAEMON_PWD}" \
    --pidfile "${DAEMON_PID}" --make-pidfile \
    --chuid "${DAEMON_USER}" \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    log_daemon_msg "Starting ${DAEMON_DESC}" "${DAEMON_NAME}"
    if [ -z "${DAEMON_USER}" ]; then
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    --chdir "${DAEMON_PWD}" \
    --pidfile "${DAEMON_PID}" --make-pidfile \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    else
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    --chdir "${DAEMON_PWD}" \
    --pidfile "${DAEMON_PID}" --make-pidfile \
    --chuid "${DAEMON_USER}" \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    fi
    log_end_msg $result
    fi
    log_end_msg $result
    return $result
    }

    do_stop() {
    local result
    log_daemon_msg "${SHUTDOWN_MSG}" "${DAEMON_NAME}"
    killproc -p "${DAEMON_PID}" "${DAEMON_PATH}"
    result=$?
    log_end_msg $result

    pidofproc -p "${DAEMON_PID}" "${DAEMON_PATH}" > /dev/null
    if [ $? -ne 0 ]; then
    log_warning_msg "${DAEMON_NAME} is not started"
    result=0
    else
    log_daemon_msg "Stopping ${DAEMON_DESC}" "${DAEMON_NAME}"
    killproc -p "${DAEMON_PID}" "${DAEMON_PATH}"
    result=$?
    log_end_msg $result
    rm "${DAEMON_PID}"
    fi
    return $result
    }

    do_restart() {
    local result
    do_stop
    result=$?
    if [ $result = 0]; then
    if [ $result = 0 ]; then
    do_start
    result=$?
    fi
  4. @andsens andsens revised this gist Apr 9, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,6 @@ do_start() {
    if [ -z "${DAEMON_USER}" ]; then
    start_daemon -n $DAEMON_NICE -p "${DAEMON_PID}" "${DAEMON_PATH}" $DAEMON_OPTS
    result=$?
    log_end_msg $result
    else
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    @@ -46,8 +45,8 @@ do_start() {
    --chuid "${DAEMON_USER}" \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    log_end_msg $result
    fi
    log_end_msg $result
    return $result
    }

  5. @andsens andsens revised this gist Apr 9, 2013. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,6 @@ STARTUP_MSG="Starting generic-prog daemon"
    SHUTDOWN_MSG="Stopping generic-prog daemon"

    DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
    DAEMON_LOG="/var/log/${DAEMON_NAME}.log"
    DAEMON_NICE=0

    [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}"
  6. @andsens andsens revised this gist Apr 9, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,7 @@ do_start() {
    --nicelevel $DAEMON_NICE \
    --chdir "${DAEMON_PWD}" \
    --pidfile "${DAEMON_PID}" --make-pidfile \
    --chuid "${DAEMON_USER}" \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    log_end_msg $result
  7. @andsens andsens revised this gist Apr 9, 2013. 1 changed file with 5 additions and 20 deletions.
    25 changes: 5 additions & 20 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -84,24 +84,9 @@ do_usage() {
    }

    case "$1" in
    start)
    do_start
    exit "$?"
    ;;
    stop)
    do_stop
    exit "$?"
    ;;
    restart)
    do_restart
    exit "$?"
    ;;
    status)
    do_status
    exit "$?"
    ;;
    *)
    do_usage
    exit 1
    ;;
    start) do_start; exit $? ;;
    stop) do_stop; exit $? ;;
    restart) do_restart; exit $? ;;
    status) do_status; exit $? ;;
    *) do_usage; exit 1 ;;
    esac
  8. @andsens andsens created this gist Apr 9, 2013.
    107 changes: 107 additions & 0 deletions debian-init.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    #!/bin/bash -e
    ### BEGIN INIT INFO
    # Provides: generic-prog
    # Required-Start: $local_fs $remote_fs $network
    # Required-Stop: $local_fs $remote_fs $network
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Generic Prog
    # Description: Generic Prog is a generic program to do generic things with
    ### END INIT INFO


    # Documentation available at
    # http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html
    # Debian provides some extra function though
    source /lib/lsb/init-functions


    DAEMON_NAME="generic-prog"
    DAEMON_USER="${DAEMON_NAME}"
    DAEMON_PATH="/usr/local/bin/generic-prog"
    DAEMON_OPTS="-c /etc/generic-prog.conf"
    DAEMON_PWD="${PWD}"

    STARTUP_MSG="Starting generic-prog daemon"
    SHUTDOWN_MSG="Stopping generic-prog daemon"

    DAEMON_PID="/var/run/${DAEMON_NAME}.pid"
    DAEMON_LOG="/var/log/${DAEMON_NAME}.log"
    DAEMON_NICE=0

    [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}"


    do_start() {
    local result
    log_daemon_msg "${STARTUP_MSG}" "${DAEMON_NAME}"
    if [ -z "${DAEMON_USER}" ]; then
    start_daemon -n $DAEMON_NICE -p "${DAEMON_PID}" "${DAEMON_PATH}" $DAEMON_OPTS
    result=$?
    log_end_msg $result
    else
    start-stop-daemon --start --quiet --oknodo --background \
    --nicelevel $DAEMON_NICE \
    --chdir "${DAEMON_PWD}" \
    --pidfile "${DAEMON_PID}" --make-pidfile \
    --exec "${DAEMON_PATH}" -- $DAEMON_OPTS
    result=$?
    log_end_msg $result
    fi
    return $result
    }

    do_stop() {
    local result
    log_daemon_msg "${SHUTDOWN_MSG}" "${DAEMON_NAME}"
    killproc -p "${DAEMON_PID}" "${DAEMON_PATH}"
    result=$?
    log_end_msg $result
    return $result
    }

    do_restart() {
    local result
    do_stop
    result=$?
    if [ $result = 0]; then
    do_start
    result=$?
    fi
    return $result
    }

    do_status() {
    local result
    status_of_proc -p "${DAEMON_PID}" "${DAEMON_PATH}" "${DAEMON_NAME}"
    result=$?
    return $result
    }

    do_usage() {
    echo $"Usage: $0 {start | stop | restart | status}"
    exit 1
    }

    case "$1" in
    start)
    do_start
    exit "$?"
    ;;
    stop)
    do_stop
    exit "$?"
    ;;
    restart)
    do_restart
    exit "$?"
    ;;
    status)
    do_status
    exit "$?"
    ;;
    *)
    do_usage
    exit 1
    ;;
    esac