In this article, we will discuss how and why it is worth creating your own asterisk.service
file for CentOS 7+ systems. If you have ready-made solutions, such as FreePBX, IssabelPBX, etc… tampering with system files can disrupt the operation of the system. However, it is worth knowing how to create your own asterisk.service
file and what benefits can result from it.
Table of Contents
1) Benefits of having your own service in systemd
First of all, it’s worth starting with the official documentation.
You will find details on how to manage services using systemd
using the systemctl
command.
Below we will describe some basic elements of the file that may affect how to create the file.
[Unit] section
- Description – description of the service
- Wants – configures “weaker” dependencies than Requires. If any of the listed units fail to start successfully, it does not affect the start of the service
- After – determines the order in which services are started (network.service -> mysqld.service); can be entered on one or more lines
[Service] section
- Type – The process started with ExecStart is the main service process
- User – user
- group – group
- ExecStart – specifies the commands or scripts to be executed when the device/service starts
- ExecReload – specifies commands or scripts to be executed after reloading the service
- ExecStop – specifies the commands or scripts to execute when the service is stopped
[Install] section
- WantedBy – list of units “weakly” dependent on this service
Of course, this is not an exhaustive setup. This should be enough for you to understand how the system works.
When configuring our clients’ servers, we came across a situation where, during the start of the operating system, the Asterisk service was loaded first, and then the database a few seconds later. This was due to the fact that after a few years, the database “swelled up” and began to load a little slower.
What was the effect?
Both services worked, Asterisk did not throw any errors, but no CDR entries were generated!
Only reloading the service with “core reload
” restored full functionality.
If you want to check what the service startup process looks like on your system, you can use the “journalctl
” command, which with the “-b
” parameter will show what has happened since the last startup.
journalctl -b
So let’s see how to avoid this problem.
HINT
Instead of following the manual step-by-step instructions below, you can also use our installer.
INSTALLER FILE
2) Delete the current asterisk file in the init.d directory
If you did “make config
” when compiling Asterisk, the system may have created the file /etc/init.d/asterisk
.
It contains a file that adds the service to autostart and controls commands like “service asterisk start
“, “service asterisk stop
“, “service asterisk restart
“, etc…
You can delete it (command “rm
“) or move it (command “mv
“) to another place on the server. We’ll just remove it.
sudo rm /etc/init.d/asterisk
Note that /etc/rc.d/init.d/asterisk is the same file as /etc/init.d/asterisk, and depending on your distribution, it may be in a different location.
3) Create your own asterisk.service file
If you don’t have the systemd-devel package
, you should start by installing it.
sudo yum -y install systemd-devel
In the directory with source files from which you compiled your own Asterisk, you will also find the file asterisk.service
.
If you do not have these source files or you have already deleted them, you can download them from the official Asterisk website.
cd /usr/src
sudo wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-16-current.tar.gz
sudo tar zxvf asterisk-16-current.tar.gz
sudo rm -rf asterisk-16-current.tar.gz
cd asterisk-*/
At this point, we don’t have the file with the Asterisk service yet, so we’ll copy it to the appropriate place.
cd /usr/src/asterisk-*
sudo cp contrib/systemd/asterisk.service /usr/lib/systemd/system/
The advantage of this solution is that we don’t have to give increased permissions (chmod +x
), as we had to do with the previous file.
As described at the beginning of this article, it is worth adjusting the asterisk.service
file to your needs.
The simplest version might look like this:
# Settings for starting the service after loading the network and, for example, the database
Wants=network-online.target
After=network-online.target network.target mysqld.target
# Change type to "simple" instead of "notify" - no need waiting for sd_notify() if compiled with Asterisk
Type=simple
# The command to reload and stop the service
ExecReload=/usr/sbin/asterisk -rx 'core reload now'
ExecStop=/usr/sbin/asterisk -rx 'core stop now'
Then add the created service to autostart.
sudo systemctl enable asterisk.service
As a result, we will create a link between /etc/systemd/system/multi-user.target.wants/asterisk.service
and /usr/lib/systemd/system/asterisk.service
.
An example file might look 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 mysqld.target
[Service]
Type=simple
Environment=HOME=/var/lib/asterisk
WorkingDirectory=/var/lib/asterisk
ExecStart=/usr/sbin/asterisk -mqf -C /etc/asterisk/asterisk.conf
ExecReload=/usr/sbin/asterisk -rx 'core reload'
ExecStop=/usr/sbin/asterisk -rx 'core stop now'
LimitCORE=infinity
Restart=always
RestartSec=4
# Prevent duplication of logs with color codes to /var/log/messages
StandardOutput=null
PrivateTmp=true
[Install]
WantedBy=multi-user.target
By comparison, the default file in FreePBX looks pretty much the same.
We hope that preparing your own asterisk.service
file for systemd
will help you solve potential problems and improve the operation of your service.
In our case, according to the purpose of these changes, the services started to load in the correct order.
4) sd_notify() notifications
If you did not have the systemd-devel
package installed when compiling Asterisk, the service will start, but its status will not be visible.
As a result we will see:
Loaded: loaded (/usr/lib/systemd/system/asterisk.service; enabled; vendor preset: disabled)
Active: activating (start)
After installing systemd-devel
, described at the beginning of this post, we can compile Asterisk again.
cd /usr/src/asterisk-*
sudo ./configure --libdir=/usr/lib64 --with-pjproject-bundled --with-jansson-bundled
sudo make
sudo make install
PBX will start sending ast_sd_notify()
(which is an overlay to sd_notify()
) and Type=notify
will be displayed correctly:
Active: active (running)
Do you really know what is happening on your PBX? Let’s try our proprietary VOIPERO software.
Setup only takes a few minutes and the system is currently completely FREE.
Read what our VOIPERO system can do in terms of live monitoring and reporting of VoIP systems based on Asterisk.