Getting Asterisk to run with systemd on Rocky 9

Hi,

I am trying to get Asterisk to start with systemd. My unit file looks like this:

[Unit]
Description=Asterisk PBX and telephony daemon.
After=network.target
#include these if asterisk need to bind to a specific IP (other than 0.0.0.0)
#Wants=network-online.target
#After=network-online.target network.target

[Service]
Type=notify
Environment=HOME=/var/lib/asterisk
#if systemd do not provide hostname and you need to use ${ENV(HOSTNAME)}
#Environment=HOSTNAME=%H
WorkingDirectory=/var/lib/asterisk
User=asterisk
Group=asterisk
PIDFile=/var/run/asterisk/asterisk.pid
ExecStart=/usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf
##ExecStart=/usr/sbin/safe_asterisk -U asterisk -G asterisk
ExecReload=/usr/sbin/asterisk -rx 'core reload'
#if /var/run is a tmpfs, this will create /var/run/asterisk on start
#RuntimeDirectory=asterisk

#Nice=0
#UMask=0002
LimitCORE=infinity
#LimitNOFILE=
Restart=always
RestartSec=4

# Prevent duplication of logs with color codes to /var/log/messages
##StandardOutput=null

PrivateTmp=true

[Install]
WantedBy=multi-user.target

When looking at the log files it seems to be working and I am able to connect to asterisk CLI. After a while I get:

[root@localhost system]# systemctl start asterisk
Job for asterisk.service failed because a timeout was exceeded.
See "systemctl status asterisk.service" and "journalctl -xeu asterisk.service" for details.
[root@localhost system]# 

Once that goes away I am still able to get to the Asterisk CLI so it’s clearly working. Any idea what a correct unit file for Asterisk should look like?

EDIT: For now this seems to work:

[Unit]
Description=Asterisk PBX and telephony daemon.
After=network.target
#include these if asterisk need to bind to a specific IP (other than 0.0.0.0)
#Wants=network-online.target
#After=network-online.target network.target

[Service]
#Type=notify
Type=forking
Environment=HOME=/var/lib/asterisk
#if systemd do not provide hostname and you need to use ${ENV(HOSTNAME)}
#Environment=HOSTNAME=%H
WorkingDirectory=/var/lib/asterisk
User=asterisk
Group=asterisk
PIDFile=/var/run/asterisk/asterisk.pid
##ExecStart=/usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf
ExecStart=/usr/sbin/safe_asterisk -U tcurve -G tcurve
ExecReload=/usr/sbin/asterisk -rx 'core reload'
#if /var/run is a tmpfs, this will create /var/run/asterisk on start
#RuntimeDirectory=asterisk

#Nice=0
#UMask=0002
LimitCORE=infinity
#LimitNOFILE=
Restart=always
RestartSec=4

# Prevent duplication of logs with color codes to /var/log/messages
##StandardOutput=null

PrivateTmp=true

[Install]
WantedBy=multi-user.target

Also as per [Asterisk-Users] vserver (Debian) - no tty: howto use /usr/sbin/safe_asterisk with "-c" for color CLI? I had to comment out the line TTY=9 in /usr/sbin/safe_asterisk. I would prefer to just use a unit file but I can’t seem to get it working.

Hi

[dovi5988] dovi5988 https://community.asterisk.org/u/dovi5988
September 11

Hi,

I am trying to get Asterisk to start with systemd. My unit file looks
like this:
[…]

Our working one is

[Unit]

Description=Asterisk PBX

Documentation=man:asterisk(8)

Wants=network-online.target

After=network-online.target

[Service]

Type=notify

ExecStart=/usr/sbin/asterisk -g -f -p -U asterisk

ExecReload=/usr/sbin/asterisk -rx ‘core reload’

Restart=on-failure

RestartSec=1

WorkingDirectory=/var/lib/asterisk

[Install]

WantedBy=multi-user.target

Hi
Here another script.

[Unit]
Description=Asterisk PBX and telephony daemon
Documentation=man:asterisk(8)
Wants=network.target
After=network.target

[Service]
Type=simple
User=root
Environment=HOME=/var/lib/asterisk
WorkingDirectory=/var/lib/asterisk
ExecStart=/usr/sbin/asterisk -f -C /etc/asterisk/asterisk.conf
ExecStop=/usr/sbin/asterisk -rx ‘core stop now’
ExecReload=/usr/bin/asterisk -rx ‘core reload’

safe_asterisk emulation

Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

I stumbled upon this post while setting asterisk on RL9 also. Here’s my working unit file, inspired from the original and those above here :

[Unit]
Description=Asterisk PBX and telephony daemon.
After=network.target
#include these if asterisk need to bind to a specific IP (other than 0.0.0.0)
#Wants=network-online.target
#After=network-online.target network.target

[Service]
Type=simple
Environment=HOME=/var/lib/asterisk
#if systemd do not provide hostname and you need to use ${ENV(HOSTNAME)}
#Environment=HOSTNAME=%H
WorkingDirectory=/var/lib/asterisk
User=asterisk
Group=asterisk
ExecStart=/usr/sbin/asterisk -f -C /etc/asterisk/asterisk.conf
ExecStop=/usr/sbin/asterisk -rx 'core stop now'
ExecReload=/usr/sbin/asterisk -rx 'core reload'
#if /var/run is a tmpfs, this will create /var/run/asterisk on start
RuntimeDirectory=asterisk

#Nice=0
#UMask=0002
LimitCORE=infinity
#LimitNOFILE=
Restart=always
RestartSec=4

# Prevent duplication of logs with color codes to /var/log/messages
StandardOutput=null
PrivateTmp=false

[Install]
WantedBy=multi-user.target

Happy PBX-ing !