debian-init.sh
· 2.1 KiB · Bash
Brut
#!/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
| 1 | #!/bin/bash -e |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: generic-prog |
| 4 | # Required-Start: $local_fs $remote_fs $network |
| 5 | # Required-Stop: $local_fs $remote_fs $network |
| 6 | # Default-Start: 2 3 4 5 |
| 7 | # Default-Stop: 0 1 6 |
| 8 | # Short-Description: Generic Prog |
| 9 | # Description: Generic Prog is a generic program to do generic things with |
| 10 | ### END INIT INFO |
| 11 | |
| 12 | |
| 13 | # Documentation available at |
| 14 | # http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptfunc.html |
| 15 | # Debian provides some extra function though |
| 16 | source /lib/lsb/init-functions |
| 17 | |
| 18 | |
| 19 | DAEMON_NAME="generic-prog" |
| 20 | DAEMON_USER="${DAEMON_NAME}" |
| 21 | DAEMON_PATH="/usr/local/bin/generic-prog" |
| 22 | DAEMON_OPTS="-c /etc/generic-prog.conf" |
| 23 | DAEMON_PWD="${PWD}" |
| 24 | |
| 25 | STARTUP_MSG="Starting generic-prog daemon" |
| 26 | SHUTDOWN_MSG="Stopping generic-prog daemon" |
| 27 | |
| 28 | DAEMON_PID="/var/run/${DAEMON_NAME}.pid" |
| 29 | DAEMON_LOG="/var/log/${DAEMON_NAME}.log" |
| 30 | DAEMON_NICE=0 |
| 31 | |
| 32 | [ -r "/etc/default/${DAEMON_NAME}" ] && source "/etc/default/${DAEMON_NAME}" |
| 33 | |
| 34 | |
| 35 | do_start() { |
| 36 | local result |
| 37 | log_daemon_msg "${STARTUP_MSG}" "${DAEMON_NAME}" |
| 38 | if [ -z "${DAEMON_USER}" ]; then |
| 39 | start_daemon -n $DAEMON_NICE -p "${DAEMON_PID}" "${DAEMON_PATH}" $DAEMON_OPTS |
| 40 | result=$? |
| 41 | log_end_msg $result |
| 42 | else |
| 43 | start-stop-daemon --start --quiet --oknodo --background \ |
| 44 | --nicelevel $DAEMON_NICE \ |
| 45 | --chdir "${DAEMON_PWD}" \ |
| 46 | --pidfile "${DAEMON_PID}" --make-pidfile \ |
| 47 | --exec "${DAEMON_PATH}" -- $DAEMON_OPTS |
| 48 | result=$? |
| 49 | log_end_msg $result |
| 50 | fi |
| 51 | return $result |
| 52 | } |
| 53 | |
| 54 | do_stop() { |
| 55 | local result |
| 56 | log_daemon_msg "${SHUTDOWN_MSG}" "${DAEMON_NAME}" |
| 57 | killproc -p "${DAEMON_PID}" "${DAEMON_PATH}" |
| 58 | result=$? |
| 59 | log_end_msg $result |
| 60 | return $result |
| 61 | } |
| 62 | |
| 63 | do_restart() { |
| 64 | local result |
| 65 | do_stop |
| 66 | result=$? |
| 67 | if [ $result = 0]; then |
| 68 | do_start |
| 69 | result=$? |
| 70 | fi |
| 71 | return $result |
| 72 | } |
| 73 | |
| 74 | do_status() { |
| 75 | local result |
| 76 | status_of_proc -p "${DAEMON_PID}" "${DAEMON_PATH}" "${DAEMON_NAME}" |
| 77 | result=$? |
| 78 | return $result |
| 79 | } |
| 80 | |
| 81 | do_usage() { |
| 82 | echo $"Usage: $0 {start | stop | restart | status}" |
| 83 | exit 1 |
| 84 | } |
| 85 | |
| 86 | case "$1" in |
| 87 | start) |
| 88 | do_start |
| 89 | exit "$?" |
| 90 | ;; |
| 91 | stop) |
| 92 | do_stop |
| 93 | exit "$?" |
| 94 | ;; |
| 95 | restart) |
| 96 | do_restart |
| 97 | exit "$?" |
| 98 | ;; |
| 99 | status) |
| 100 | do_status |
| 101 | exit "$?" |
| 102 | ;; |
| 103 | *) |
| 104 | do_usage |
| 105 | exit 1 |
| 106 | ;; |
| 107 | esac |
| 108 |