Merged revisions 115285 via svnmerge from
[asterisk/asterisk.git] / contrib / init.d / rc.redhat.asterisk
1 #!/bin/sh
2 # $Id$
3 #
4 # asterisk    Starts, Stops and Reloads Asterisk.
5 #
6 # chkconfig: 2345 90 60
7 # description: Asterisk PBX and telephony daemon.
8 # processname: asterisk
9 # pidfile: /var/run/asterisk.pid
10 #
11 # Thu Nov 17 2005 Gregory Boehnlein <damin@nacs.net>
12 # - Updated Version to 1.3
13 # - Reversed behavior of LD_ASSUME_KERNEL=2.4.1
14 # - Added detailed failure messages
15 #
16 # Sun Jul 18 2004 Gregory Boehnlein <damin@nacs.net>
17 # - Updated Version to 1.2
18 # - Added test for safe_asterisk
19 # - Verified SIGTERM issued by "killproc" ensures "stop gracefully"
20 # - Added support for -U and -G command line options
21 # - Modified "reload" to call asterisk -rx 'reload' 
22
23 # Use this option to specify a different configuration directory
24 #AST_CONFIG=/etc/asterisk
25
26 # Installation directory
27 AST_SBIN=/usr/sbin
28
29 # Uncomment the following and set them to the user/groups that you
30 # want to run Asterisk as. NOTE: this requires substantial work to
31 # be sure that Asterisk's environment has permission to write the
32 # files required  for  its  operation, including logs, its comm
33 # socket, the asterisk database, etc.
34 #AST_USER="asterisk"
35 #AST_GROUP="asterisk"
36
37 # Source function library.
38 . /etc/rc.d/init.d/functions
39
40 if ! [ -x $AST_SBIN/asterisk ] ; then
41         echo "ERROR: $AST_SBIN/asterisk not found"
42         exit 0
43 fi
44
45 if ! [ -d $AST_CONFIG ] ; then
46         echo "ERROR: $AST_CONFIG directory not found"
47         exit 0
48 fi
49
50 # Uncomment this ONLY if you know what you are doing.
51 # export LD_ASSUME_KERNEL=2.4.1
52
53 # Full path to asterisk binary
54 DAEMON=$AST_SBIN/asterisk
55
56 # Full path to safe_asterisk script
57 SAFE_ASTERISK=$AST_SBIN/safe_asterisk
58
59 # Allow configuration overrides in /etc/sysconfig/asterisk
60 CONFIG0=`readlink $0`
61 if [ "$CONFIG0" = "" ]; then
62         CONFIGFILE=/etc/sysconfig/`basename $0`
63 else
64         CONFIGFILE=/etc/sysconfig/`basename $CONFIG0`
65 fi
66 [ -x $CONFIGFILE ] && . $CONFIGFILE
67
68 RETVAL=0
69
70 start() {
71         # Check if Asterisk is already running.  If it is, then bug out, because
72         # starting safe_asterisk when Asterisk is running is very bad.
73         VERSION=`${AST_SBIN}/asterisk -rx 'core show version'`
74         if [ "${VERSION:0:8}" = "Asterisk" ]; then # otherwise "Unable t"
75                 echo "Asterisk is already running."
76                 exit 1
77         fi
78
79         # Start daemons.
80         echo -n $"Starting asterisk: "
81         if [ -f $SAFE_ASTERISK ] ; then
82                 DAEMON=$SAFE_ASTERISK
83         fi
84         if [ $AST_USER ] ; then
85                 ASTARGS="-U $AST_USER"
86         fi
87         if [ $AST_GROUP ] ; then
88                 ASTARGS="$ASTARGS -G $AST_GROUP"
89         fi
90         if [ $AST_CONFIG ]; then
91                 ASTARGS="$ASTARGS -C $AST_CONFIG"
92         fi
93         daemon $DAEMON $ASTARGS
94         RETVAL=$?
95         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/asterisk
96         echo
97         return $RETVAL
98 }
99
100 stop() {
101         # Stop daemons.
102         echo -n $"Shutting down asterisk: "
103         killproc asterisk
104         RETVAL=$?
105         [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/asterisk
106         echo
107         return $RETVAL
108 }
109
110 restart() {
111         stop
112         start
113 }
114
115 reload() {
116         $DAEMON -rx 'reload' > /dev/null 2> /dev/null
117 }
118
119 # See how we were called.
120 case "$1" in
121   start)
122         start
123         ;;
124   stop)
125         stop
126         ;;
127   restart)
128         restart
129         ;;
130   reload)
131         reload
132         ;;
133   condrestart)
134         [ -f /var/lock/subsys/asterisk ] && restart || :
135         ;;
136   status)
137         status asterisk
138         ;;
139   *)
140         echo "Usage: asterisk {start|stop|restart|reload|condrestart|status}"
141         exit 1
142 esac
143
144 exit $?