| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# asterisk start the asterisk PBX |
| 4 |
# (c) Mark Purcell <[email protected]> |
| 5 |
# (c) Tzafrir Cohen <[email protected]> |
| 6 |
# (c) Faidon Liambotis <[email protected]> |
| 7 |
# |
| 8 |
# This package is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License as published by |
| 10 |
# the Free Software Foundation; either version 2 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
### BEGIN INIT INFO |
| 14 |
# Provides: asterisk |
| 15 |
# Required-Start: $remote_fs |
| 16 |
# Required-Stop: $remote_fs |
| 17 |
# Should-Start: $syslog $network $named mariadb postgresql dahdi |
| 18 |
# Should-Stop: $syslog $network $named mariadb postgresql |
| 19 |
# Default-Start: 2 3 4 5 |
| 20 |
# Default-Stop: 0 1 6 |
| 21 |
# Short-Description: Asterisk PBX |
| 22 |
# Description: Controls the Asterisk PBX |
| 23 |
### END INIT INFO |
| 24 |
|
| 25 |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
| 26 |
NAME=asterisk |
| 27 |
USER=$NAME |
| 28 |
GROUP=$USER |
| 29 |
DAEMON=/usr/sbin/$NAME |
| 30 |
CANARY=/usr/sbin/astcanary |
| 31 |
DESC="Asterisk PBX" |
| 32 |
PIDFILE="/var/run/asterisk/asterisk.pid" |
| 33 |
ASTSAFE_PIDFILE="/var/run/asterisk/asterisk_safe.pid" |
| 34 |
UMASK=007 # by default |
| 35 |
#MAXFILES=1024 # (the system default) |
| 36 |
|
| 37 |
. /lib/lsb/init-functions |
| 38 |
|
| 39 |
# by default: use real-time priority |
| 40 |
PARAMS="" |
| 41 |
CHDIR_PARM="" |
| 42 |
AST_REALTIME="yes" |
| 43 |
RUNASTERISK="yes" |
| 44 |
AST_DUMPCORE="no" |
| 45 |
AST_DUMPCORE_DIR="/var/spool/asterisk" # only used if AST_DUMPCORE != no |
| 46 |
# core_pattern. See: http://lxr.linux.no/source/Documentation/sysctl/kernel.txt |
| 47 |
#CORE_PATTERN= |
| 48 |
if [ -r /etc/default/$NAME ]; then . /etc/default/$NAME; fi |
| 49 |
|
| 50 |
if [ "$RUNASTERISK" != "yes" ];then |
| 51 |
echo "Asterisk not yet configured. Edit /etc/default/asterisk first." |
| 52 |
exit 0 |
| 53 |
fi |
| 54 |
|
| 55 |
if [ "$AST_REALTIME" != "no" ] |
| 56 |
then |
| 57 |
PARAMS="$PARAMS -p" |
| 58 |
fi |
| 59 |
|
| 60 |
if [ "$AST_DUMPCORE" != "no" ] |
| 61 |
then |
| 62 |
PARAMS="$PARAMS -g" |
| 63 |
if [ "$CORE_PATTERN" != '' ] |
| 64 |
then |
| 65 |
echo "$CORE_PATTERN" >/proc/sys/kernel/core_pattern |
| 66 |
fi |
| 67 |
if [ -d "$AST_DUMPCORE_DIR" ] |
| 68 |
then |
| 69 |
CHDIR_PARM="--chdir $AST_DUMPCORE_DIR" |
| 70 |
fi |
| 71 |
fi |
| 72 |
|
| 73 |
if [ "x$USER" = "x" ] |
| 74 |
then |
| 75 |
echo "Error: empty USER name" |
| 76 |
exit 1 |
| 77 |
fi |
| 78 |
if [ `id -u "$USER"` = 0 ] |
| 79 |
then |
| 80 |
echo "Starting as root not supported." |
| 81 |
exit 1 |
| 82 |
fi |
| 83 |
PARAMS="$PARAMS -U $USER" |
| 84 |
|
| 85 |
if [ "x$AST_DEBUG_PARAMS" = x ] |
| 86 |
then |
| 87 |
AST_DEBUG_PARAMS=-cvvvvvddddd |
| 88 |
fi |
| 89 |
if [ "$RUNASTSAFE" = "yes" ];then |
| 90 |
# The value of WRAPPER_DAEMON in can be set in /etc/default/asterisk |
| 91 |
WRAPPER_DAEMON=${WRAPPER_DAEMON:-/usr/sbin/safe_asterisk} |
| 92 |
REALDAEMON="$WRAPPER_DAEMON" |
| 93 |
else |
| 94 |
REALDAEMON="$DAEMON" |
| 95 |
fi |
| 96 |
|
| 97 |
test -x $DAEMON || exit 0 |
| 98 |
|
| 99 |
for dir in /var/run/asterisk /var/log/asterisk /var/log/asterisk/cdr-csv /var/log/asterisk/cdr-custom; do |
| 100 |
[ -d $dir ] || install -d -o $USER -g $GROUP $dir |
| 101 |
[ -x /sbin/restorecon ] && /sbin/restorecon $dir |
| 102 |
done |
| 103 |
|
| 104 |
set -e |
| 105 |
|
| 106 |
if [ "$UMASK" != '' ] |
| 107 |
then |
| 108 |
umask $UMASK |
| 109 |
fi |
| 110 |
|
| 111 |
# allow changing the per-process open files limit: |
| 112 |
if [ "$MAXFILES" != '' ] |
| 113 |
then |
| 114 |
ulimit -n $MAXFILES |
| 115 |
fi |
| 116 |
|
| 117 |
status() { |
| 118 |
status_of_proc -p "$PIDFILE" "$NAME" "$DESC" && return 0 || return $? |
| 119 |
} |
| 120 |
|
| 121 |
safe_status() { |
| 122 |
status_of_proc -p "$ASTSAFE_PIDFILE" "$NAME" "$DESC" && return 0 || return $? |
| 123 |
} |
| 124 |
|
| 125 |
asterisk_rx() { |
| 126 |
if ! status >/dev/null; then return 0; fi |
| 127 |
|
| 128 |
# if $HOME is set, asterisk -rx writes a .asterisk_history there |
| 129 |
( |
| 130 |
unset HOME |
| 131 |
$DAEMON -rx "$1" |
| 132 |
) |
| 133 |
} |
| 134 |
|
| 135 |
case "$1" in |
| 136 |
debug) |
| 137 |
# we add too many special parameters that I don't want to skip |
| 138 |
# accidentally. I'm afraid that skipping -U once may cause |
| 139 |
# confusing results. I also want to maintain the user's choice |
| 140 |
# of -p |
| 141 |
echo "Debugging $DESC: " |
| 142 |
$DAEMON $PARAMS $AST_DEBUG_PARAMS |
| 143 |
exit 0 |
| 144 |
;; |
| 145 |
start) |
| 146 |
if [ "$RUNASTSAFE" = "yes" ];then |
| 147 |
if safe_status >/dev/null; then |
| 148 |
echo "$DESC is already running. Use restart." |
| 149 |
exit 0 |
| 150 |
fi |
| 151 |
echo -n "Starting $DESC: " |
| 152 |
export ASTSAFE_FOREGROUND=1 |
| 153 |
start-stop-daemon --start --group $GROUP \ |
| 154 |
--background --make-pidfile \ |
| 155 |
$CHDIR_PARM --pidfile "$ASTSAFE_PIDFILE" \ |
| 156 |
--exec $REALDAEMON -- $PARAMS |
| 157 |
else |
| 158 |
if status >/dev/null; then |
| 159 |
echo "$DESC is already running. Use restart." |
| 160 |
exit 0 |
| 161 |
fi |
| 162 |
echo -n "Starting $DESC: " |
| 163 |
start-stop-daemon --start --group $GROUP --pidfile "$PIDFILE" \ |
| 164 |
$CHDIR_PARM \ |
| 165 |
--exec $REALDAEMON -- $PARAMS > /dev/null |
| 166 |
fi |
| 167 |
echo "$NAME." |
| 168 |
;; |
| 169 |
stop) |
| 170 |
echo -n "Stopping $DESC: $NAME" |
| 171 |
# Try gracefully. |
| 172 |
# This may hang in some cases. Specifically, when the asterisk |
| 173 |
# processes is stopped. No bother to worry about cleanup: |
| 174 |
# it will either fail or die when asterisk dies. |
| 175 |
( asterisk_rx 'core stop now' > /dev/null 2>&1 & ) & |
| 176 |
if [ "$RUNASTSAFE" = "yes" ];then |
| 177 |
# If you're switching back and forth between RUNASTSAFE |
| 178 |
# you may get a warning about a stale pidfile. Ignore |
| 179 |
# it. |
| 180 |
start-stop-daemon --stop --quiet --oknodo \ |
| 181 |
--pidfile $ASTSAFE_PIDFILE |
| 182 |
rm -f $ASTSAFE_PIDFILE |
| 183 |
fi |
| 184 |
# Sometimes during a quick restart cycle, the 'core stop now' |
| 185 |
# from above won't reach the daemon -- perhaps it wasn't |
| 186 |
# listening yet. At this point we want TERM to kick in. |
| 187 |
# In any case, we must be certain that it is stopped before we |
| 188 |
# exit the "stop" case; otherwise a "restart" might complete |
| 189 |
# with no asterisk running at all (because of the status checks |
| 190 |
# in "start"). |
| 191 |
start-stop-daemon --stop --quiet --oknodo --retry=8/TERM/30/KILL/5 \ |
| 192 |
--exec $DAEMON --pidfile=$PIDFILE |
| 193 |
echo "." |
| 194 |
;; |
| 195 |
reload) |
| 196 |
echo "Reloading $DESC configuration files." |
| 197 |
asterisk_rx 'module reload' |
| 198 |
;; |
| 199 |
logger-reload) |
| 200 |
asterisk_rx 'logger reload' |
| 201 |
;; |
| 202 |
extensions-reload|dialplan-reload) |
| 203 |
echo "Reloading $DESC configuration files." |
| 204 |
asterisk_rx 'dialplan reload' |
| 205 |
;; |
| 206 |
restart-convenient) |
| 207 |
asterisk_rx 'core restart when convenient' |
| 208 |
;; |
| 209 |
restart|force-reload) |
| 210 |
$0 stop |
| 211 |
$0 start |
| 212 |
;; |
| 213 |
status) |
| 214 |
status |
| 215 |
exit $? |
| 216 |
;; |
| 217 |
*) |
| 218 |
N=/etc/init.d/$NAME |
| 219 |
echo "Usage: $N {start|stop|restart|reload|status|debug|logger-reload|extensions-reload|restart-convenient|force-reload}" >&2 |
| 220 |
exit 1 |
| 221 |
;; |
| 222 |
esac |
| 223 |
|
| 224 |
exit 0 |