Automatically starting Asterisk at boot time?

Has anyone written an asterisk startup/shutdown/status script?

The one I am talking about should be in the /etc/rc.d/init.d/ directory. See example below.

-Ramon


SPOOL=/var/spool/hylafax

test -f $SPOOL/etc/setup.cache || {
cat<<EOF

FATAL ERROR: $SPOOL/etc/setup.cache is missing!

The file $SPOOL/etc/setup.cache is not present. This
probably means the machine has not been setup using the faxsetup(8C)
command. Read the documentation on setting up HylaFAX before you
startup a server system.

EOF
exit 1
}
. $SPOOL/etc/setup.cache

Just in case these are not in setup.cache yet

if [ -z “$HFAXD_SERVER” ]; then
HFAXD_SERVER=yes
fi
if [ -z “$FAXQ_SERVER” ]; then
FAXQ_SERVER=yes
fi
if [ -z “$HFAXD_OLD_PROTOCOL” ]; then
HFAXD_OLD_PROTOCOL=no
fi
if [ -z “$HFAXD_SNPP_SERVER” ]; then
HFAXD_SNPP_SERVER=yes
fi

IS_ON=/etc/chkconfig # NB: chkconfig is IRIX- and Linux-specific
FAXQ=$SBIN/faxq
HFAXD=$LIBEXEC/hfaxd
FAXQUIT=$SBIN/faxquit
FAXPORT=hylafax # designated port for new protocol
SNPPPORT=snpp # official port for SNPP

if test ! -x $IS_ON ; then
IS_ON=true
fi

if $IS_ON verbose ; then
ECHO=echo
else # For a quiet startup and shutdown
ECHO=:
fi

Figure out which brand of echo we have and define prompt

and printf shell functions accordingly. Note that we

assume that if the System V-style echo is not present,

then the BSD printf program is available. These functions

are defined here so that they can be tailored on a per-site,

etc. basis.

if [ echo foo\\\c@ = “foo@” ]; then
# System V-style echo supports \r
# and \c which is all that we need
printf()
{
$ECHO “$\c"
}
elif [ “echo -n foo@” = “foo@” ]; then
# BSD-style echo; use echo -n to get
# a line without the trailing newline
printf()
{
$ECHO -n "$

}
else
# something else; do without
printf()
{
$ECHO “$*”
}
fi

killall -SIGNAL process-name

Emulate the necessary functionality of the

killall program

killall()
{
# NB: ps ax should give an error on System V, so we try it first!
pid="ps ax 2>/dev/null | $AWK \"\ /[\/ (]$2[ )]/ {print \\$1;} /[\/ ]$2\$/ {print \\$1;}\"
test “$pid” ||
pid=”ps -e 2>/dev/null | $AWK \"/ $2[ ]*\$/ {print \\$1;}\""
test “$pid” && kill $1 $pid; return
}

case $1 in
’start’)
if $IS_ON fax && test -x $FAXQ; then
if test $FAXQ_SERVER = yes ; then
killall -15 faxq
fi
if test $HFAXD_SERVER = yes ; then
killall -15 hfaxd
fi
printf “HylaFAX:“
if test $FAXQ_SERVER = yes ; then
$FAXQ; printf " faxq"
fi
if test $HFAXD_SERVER = yes ; then
HFAXD=”$HFAXD -i $FAXPORT"
HFAXMSG=” hfaxd"
if [ $HFAXD_OLD_PROTOCOL = yes ]; then
HFAXD="$HFAXD -o 4557"
HFAXMSG="$HFAXMSG (with old protocol"
else
HFAXMSG="$HFAXMSG (without old protocol"
fi
if [ $HFAXD_SNPP_SERVER = yes ]; then
HFAXD="$HFAXD -s $SNPPPORT"
HFAXMSG="$HFAXMSG & with SNPP support)“
else
HFAXMSG=”$HFAXMSG & without SNPP support)"
fi
$HFAXD ; printf "$HFAXMSG"
fi
if test $FAXQ_SERVER != yes -a $HFAXD_SERVER != yes ; then
printf " not started (script disabled by configure)"
fi
$ECHO "."
fi
;;
‘stop’)
$ECHO “Stopping HylaFAX Servers.”
$FAXQUIT >/dev/null 2>&1
killall -15 hfaxd
;;
‘start_msg’)
$ECHO “Starting HylaFAX Servers.”
;;
‘stop_msg’)
$ECHO “Stopping HylaFAX Servers.”
;;
*)
$ECHO “usage: hylafax {start|stop|start_msg|stop_msg}”
;;
esac

Have you looked under contrib/init.d/ of your source tree?

Obviously not. :wink:

It looks great!

Thanks,

-Ramon

‘make config’ will auto install the scripts

I’ve the same problem… i launched

make config

but I’ve the following error:

[code]voip-pbx:/usr/src/asterisk-1.2.15 # make config
build_tools/make_version_h > include/asterisk/version.h.tmp
if cmp -s include/asterisk/version.h.tmp include/asterisk/version.h ; then echo; else
mv include/asterisk/version.h.tmp include/asterisk/version.h ;
fi

rm -f include/asterisk/version.h.tmp
if [ -d /etc/rc.d/init.d ]; then
install -m 755 contrib/init.d/rc.redhat.asterisk /etc/rc.d/init.d/asterisk;
/sbin/chkconfig --add asterisk;
elif [ -d /etc/init.d ]; then
install -m 755 init.asterisk /etc/init.d/asterisk;
fi
install: cannot stat `init.asterisk’: No such file or directory
make: *** [config] Error 1[/code]

someone can help me? :cry:

Don’t use the “AUTOSTART=” or something similar in the config. Based on experience with FC5 and Deb3.1, everytime I reboot the server via init 6 and similar, asterisk shuts down and start the restart process but eventually will not go thru. Instead asterisk will start again.

Note that some distros asterisk scripts will break hard if you run asterisk manually as root, before running the script… In SuSE, asterisk runs as “asterisk” user from the script, and if you’re like me and first tried it as root to “test drive” it, all your log files get created with root permissions and prevent the startup script from working… Have to fix all the “bad” permissions…

ok… I understand that the problem is linked to file-permissions and to host-users…
you suggest me to repair “bad” permissions…
but I don’t understand on which files! :cry:

I tried another solution: I would like to copy /contrb/init.d/rc.suse.asterisk in /etc/rc.d and then Enable Asterisk at boot using Yast…
bu there isn’t rc.suse.asterisk in my Asterisk-source-files!!! I’m using 1.2.15 version…
any tip???

[quote=“menostress”]ok… I understand that the problem is linked to file-permissions and to host-users…
you suggest me to repair “bad” permissions…
but I don’t understand on which files! :cry:[/quote]

/var/log/asterisk

Make sure all the subdirectories and files are owned by your asterisk user.

[quote=“menostress”]I tried another solution: I would like to copy /contrb/init.d/rc.suse.asterisk in /etc/rc.d and then Enable Asterisk at boot using Yast…
bu there isn’t rc.suse.asterisk in my Asterisk-source-files!!! I’m using 1.2.15 version…
any tip???[/quote]

Oh, you’re using SuSE too… All you have to do is startup yast2 (or yast, depending if you like GUI or CLI), and turn on the Asterisk system service. It handles all the pesky little details for you!

I did some steps:

I needed [color=blue]rc.suse.asterisk[/color], usually it is in this directory: [color=blue]asterisk-version/contrib/init.d[/color]
Since it was not inside *1.2-source-cod, I take it from *1.4 and I copied it into [color=blue]/etc/rc.d[/color]!

Then, I modified /asterisk-version/Makefile adding those lines:
[color=brown]elif [ -f /etc/SuSE-release -o -f /etc/novell-release ]; then
$(INSTALL) -m 755 contrib/init.d/rc.suse.asterisk /etc/init.d/asterisk;
/sbin/chkconfig --add asterisk; [/color]

With this modifies, now I can see asterisk inside Yast->Sistem->Runlevel editor
But when I try to Enable asterisk for the boot… I’ve error like this:

[code]Starting asterisk: Asterisk ended with exit status 1
Asterisk died with code 1.
Automatically restarting Asterisk.

Asterisk ended with exit status 1
Asterisk died with code 1.
Automatically restarting Asterisk.
Asterisk ended with exit status 1
Asterisk died with code 1.
Automatically restarting Asterisk.
Asterisk ended with exit status 1
Asterisk died with code 1.
Automatically restarting Asterisk.
Asterisk ended with exit status 1
Asterisk died with code 1.
Automatically restarting Asterisk.
Asterisk ended with exit status 1
…[/code]

and after the boot… Asterisk is not in run-state!!!

:cry:

tail /var/log/asterisk/full will be useful here