[SOLVED] Centos 7 compatible init.d or systemd script for Asterisk 13

It’s just not working for Centos:

# systemctl start asterisk
Job for asterisk.service failed because a configured resource limit was exceeded. See "systemctl status asterisk.service" and "journalctl -xe" for details.
[root@rgir-asterisk-1 asterisk]# systemctl status asterisk
▒ asterisk.service - LSB: Asterisk PBX
   Loaded: loaded (/etc/rc.d/init.d/asterisk)
   Active: failed (Result: resources) since Wed 2016-04-13 00:45:52 UTC; 5s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1487 ExecStart=/etc/rc.d/init.d/asterisk start (code=exited, status=0/SUCCESS)
 
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: Starting LSB: Asterisk PBX...
Apr 13 00:45:52 rgir-asterisk-1 asterisk[1487]: Starting asterisk:
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: PID file /var/run/asterisk/asterisk.pid not readable (yet?) after start.
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: asterisk.service never wrote its PID file. Failing.
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: Failed to start LSB: Asterisk PBX.
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: Unit asterisk.service entered failed state.
Apr 13 00:45:52 rgir-asterisk-1 systemd[1]: asterisk.service failed.

I could do something hackish, but I’d rather a proper script, and not knowing systemd that well I’d feel more comfortable using a prefab solution.

Thanks in advance.

1 Like

I solved this by:

mv /etc/init.d/asterisk ~/asterisk.init.d.bak

vi /etc/systemd/system/asterisk.service

… and I put in …

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

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

# safe_asterisk emulation
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

… and then running …

systemctl deamon-reload
systemctl enable asterisk
systemctl restart asterisk

Hope this helps someone.

With minor modifications, the service comes from here:

Source

3 Likes

@Furious-George: Thank you very much. This really helped me out. I was getting this error:

asterisk.service - LSB: Asterisk PBX
Loaded: loaded (/etc/init.d/asterisk; bad; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2017-04-06 20:42:10 WAT; 8s ago
Docs: man:systemd-sysv-generator(8)
Process: 22804 ExecStart=/etc/init.d/asterisk start (code=exited, status=2)
systemd[1]: Starting LSB: Asterisk PBX…
asterisk[22804]: * Starting Asterisk PBX: asterisk
asterisk[22804]: start-stop-daemon: unable to start /usr/sbin/ (Permission denied)
Apr 06 20:42:10 vps.ui.edu.ng systemd[1]: asterisk.service: Control process exited, code=exited status=2
systemd[1]: Failed to start LSB: Asterisk PBX.
systemd[1]: asterisk.service: Unit entered failed state.
systemd[1]: asterisk.service: Failed with result ‘exit-code’.

But after following your outline above, it runs smoothly. But I’m yet to understand the last two lines displayed below :
● asterisk.service - Asterisk PBX and telephony daemon
Loaded: loaded (/etc/systemd/system/asterisk.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2017-04-06 21:39:22 WAT; 1s ago
Docs: man:asterisk(8)
Process: 24081 ExecStop=/usr/sbin/asterisk -rx core stop now (code=exited, status=217/USER)
Process: 24080 ExecStart=/usr/sbin/asterisk -f -C /etc/asterisk/asterisk.conf (code=exited, status=217/USER)
Main PID: 24080 (code=exited, status=217/USER)
systemd[1]: asterisk.service: Unit entered failed state.
asterisk.service: Failed with result ‘exit-code’.

What should be done the clear this new status errors:
systemd[1]: asterisk.service: Unit entered failed state.
asterisk.service: Failed with result ‘exit-code’.

Thank you.

Just an added information: I failed to mention I was running asterisk 14.3.1 on ubuntu 16.04 server.

hello everyone
i have the same problem
when i followed your steps, this message kept repeating

/usr/sbin/safe_asterisk: line 171: 5431 Illegal instruction (core dumped) nice -n $PRIORITY “${ASTSBINDIR}/asterisk” -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY}
cat: /var/run/asterisk/asterisk.pid: No such file or directory

any ideas??

Thank you very much. I had to adjust this a little bit to fit my installation.

ExecReload needed to be as follows (/sbin not /bin):

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

The ‘-r’ is redundant. It is ‘implied’ by ‘-x.’