Advice on how to start/stop Asterisk

Hello,

I had lately a very unpleasant experience when I restarted an Asterisk instance after recompiling it.

As usual, I entered ‘service asterisk start’ on my console and quickly got a prompt back asking me for the next command to enter.
Then I type ‘rasterisk’ and got a scary ‘Could not connect to …’ reply.
I retried several times this ‘rasterisk’ command and constantly got the same error reply.

The reason why I couldn’t connect to Asterisk console was simply that Asterisk failed to start.
The reason why Asterisk failed to start is not important here as what matters for me is why would ‘service asterisk start’ apparently succeeds in the foreground when Asterisk fails to start in the background.

Ideally, I would prefer to use a command that takes a bit longer to run but would also alert me when something goes wrong before Asterisk considers itself to be ready.

Thinking back about, I’m realizing I use routinely this /usr/sbin/service (on Debian host) without knowing much about its origin, strengths or weaknesses.

Is there a better way to (re-)start Asterisk while being notified of any fatal error keeping it to reach the “Asterisk ready” stage ?

This current ‘service asterisk start; sleep 10; rasterisk’ process is not so bad interactively but when you need to script , something more atomic, that either fails or succeeds, may be better.

Regards

This is one of many reasons why systemd was created: service startup can be a complex thing, and many cases a robust mechanism is needed where the daemon can notify the service manager that it has successfully reached running status.

If you look at the Debian package for Asterisk, for example, it includes a suitable service definition in /usr/lib/systemd/system/asterisk.service, which contains the lines

Type=notify
ExecStart=/usr/sbin/asterisk -g -f -p -U asterisk
ExecReload=/usr/sbin/asterisk -rx 'core reload'

The Debian Asterisk package has been built to call the systemd notification routine (“Type=notify” tells systemd it will do this) so that it can reliably report successful startup.

You can check if your asterisk binary was compiled with systemd support by running:

ldd /usr/sbin/asterisk | grep systemd

Then you should see something like:

libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007fa0e5b64000)

But if it isn’t there, you can install some more Debian packages:

apt-get install libsystemd-dev pkg-config

…then reconfigure and recompile asterisk so it picks up systemd the next time around.

I’ve got this

ldd /usr/sbin/asterisk | grep systemd
	libsystemd.so.0 => /lib/x86_64-linux-gnu/libsystemd.so.0 (0x00007f128a046000)

and this

# find / -name asterisk.service
/run/systemd/generator.late/graphical.target.wants/asterisk.service
/run/systemd/generator.late/multi-user.target.wants/asterisk.service
/run/systemd/generator.late/asterisk.service
/etc/systemd/system/multi-user.target.wants/asterisk.service
/sys/fs/cgroup/system.slice/asterisk.service
/var/lib/systemd/deb-systemd-helper-masked/asterisk.service
/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/asterisk.service
/usr/src/asterisk-20.8.1/contrib/systemd/asterisk.service
/usr/src/asterisk-asterisk-c31f1c6/contrib/systemd/asterisk.service

To me, a properly installed asterisk.service file is missing, right ?
Does it explain why a start failure was not reported when entering ‘service asterisk start’ or is it required to use ‘systemctl’ to get full reporting of such errors ?

How about the output from systemctl status asterisk ?

Note that systemctl only gives you a small set of log messages. To examine the full log, you use journalctl.

systemctl’s output is:

asterisk.service - LSB: Asterisk PBX
     Loaded: loaded (/etc/init.d/asterisk; generated)
    Drop-In: /etc/systemd/system/asterisk.service.d
             └─override.conf
     Active: active (running) since Fri 2024-09-13 21:41:08 CEST; 3 days ago
       Docs: man:systemd-sysv-generator(8)
    Process: 169994 ExecStart=/etc/init.d/asterisk start (code=exited, status=0/SUCCESS)
      Tasks: 81 (limit: 7069)
     Memory: 194.7M
        CPU: 3h 13min 57.768s
     CGroup: /system.slice/asterisk.service
             ├─170000 /usr/sbin/asterisk -p -U asterisk
             └─170001 astcanary /var/run/asterisk/alt.asterisk.canary.tweet.tweet.tweet 170000

Comparing this with other Asterisk instance, I see an asterisk.service file is missing on this specific Asterisk instance.
I copied such asterisk.service file from another box and I’ll restart Asterisk if this new asterisk.service which has a type=notify statement in its Service section brings the atomicity I’m looking for.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.