March 10, 2021

299 words 2 mins read

Kamailio systemd or init.d scripts

Kamailio systemd or init.d scripts

After every manual installation of Kamailio from git, it is best practice to create an init.d or systemd script to easily start, stop or restart the Kamailio service. A lot of people are still in the habit of creating these scripts manually because it probably was a step they added in their installation scripts years back. Yes, we are all creatures of habit but for a while now there is reason for a change in this habit.

Starting with the release of Kamailio 5.2, the installation of these scripts got a lot easier. Systemd and init.d scripts are now available for Debian/Ubuntu and Centos.

For example, the generated systemd file for the default manual installation location /usr/local/etc/kamailio/ is:

[Unit]
Description=kamailio (OpenSER) - the Open Source SIP Server
Wants=network-online.target
After=network-online.target

[Service]
Type=forking
User=kamailio
Group=kamailio
Environment='CFGFILE=/usr/local/etc/kamailio/kamailio.cfg'
Environment='SHM_MEMORY=64'
Environment='PKG_MEMORY=8'
EnvironmentFile=-/etc/default/kamailio
EnvironmentFile=-/etc/default/kamailio.d/*
# PIDFile requires a full absolute path
PIDFile=/run/kamailio/kamailio.pid
# ExecStart requires a full absolute path
ExecStart=/usr/local/sbin/kamailio -P /run/kamailio/kamailio.pid -f $CFGFILE -m $SHM_MEMORY -M $PKG_MEMORY
Restart=on-failure
# /run/kamailio in tmpfs
RuntimeDirectory=kamailio
RuntimeDirectoryMode=0770
# necessary for chown of control files e.g. for jsonrpcs and ctl modules
AmbientCapabilities=CAP_CHOWN

[Install]
WantedBy=multi-user.target

The memory allocation is the only thing you might need to change if you believe your environment requires it.

To generate and install the systemd script, after the installation of Kamailio with make install , on a Debian server, you can run

make install-systemd-debian

On a Centos server it would be

make install-systemd-centos

To install the init.d script no Debian/Ubuntu, run

make install-initd-debian

For Centos

make install-initd-centos

This feature was backported to Kamailio 5.1 in March of 2019, so those still installing version 5.1 can also benefit from it.

So all those still generating their own init.d and systemd scripts, maybe it is time to update your Kamailio installation scripts.

Keep flying Kamailio 😎