Asterisk systemd startup script

The systemd startup script “out of the box” (at least up to version 15.5) for Asterisk does not work properly: on rebooting the server the ownership of /run/asterisk (symlink /var/run/asterisk) reverts to root.root instead of asterisk.asterisk

The reason for this is that the o/s removes and re-creates the /run/asterisk directory on reboot. The solution is to place a “RuntimeDirectory=” entry in the systemd service script:

/usr/lib/systemd/system/asterisk.service

The “RuntimeDirectory=” needs a directory name relative to /run so you can’t do this:

RuntimeDirectory=/run/asterisk

nor this:

RuntimeDirectory=/var/run/asterisk

You need to do this:

RuntimeDirectory=asterisk

Here is my fully working script (/usr/lib/systemd/system/asterisk.service):

[Unit]
Description=Asterisk PBX and telephony daemon.
Wants=network.target
After=network.target

[Service]
Type=simple
User=asterisk
Group=asterisk
RuntimeDirectory=asterisk
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/sbin/asterisk -rx 'core reload'

# safe_asterisk emulation
Restart=always
RestartSec=10

#Nice=0
#UMask=0002
LimitCORE=infinity
#LimitNOFILE=

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

PrivateTmp=true

[Install]
WantedBy=multi-user.target