How to configure the Janus WebRTC server as a service with systemd in Ubuntu 18.04

In previous tutorials, we explained how to build the Janus WebRTC gateway in your Ubuntu 18.04 server pretty easily. Now when you work in production with the mentioned tool, you surely don't want to keep the process running on the terminal in your PC with PuTTY or some SSH tool, instead the most recommendable thing to do is to create a service that runs the binary of Janus.

The first thing that you need to do is to create the systemd file with the following command, in this case we will name the service webrtcserver:

sudo nano /etc/systemd/system/webrtcserver.service

Then, paste the following content on the terminal. Note that you should replace the values of:

  • WorkingDirectory: the directory where the janus binary should have as the current directory.
  • ExecStart: the path to the janus binary, you may remove /usr/bin/sudo if you don't want to run the process with more privileges.

The content of the file will be the following one:

[Unit]
Description=My Janus WebRTC Server
After=network.target

[Service]
User=root
Nice=1
Restart=on-abnormal
LimitNOFILE=65536
# If you need to specify the current working directory of the
WorkingDirectory=/opt/janus/share/janus/recordings
ExecStart=/usr/bin/sudo /opt/janus/bin/janus

[Install]
WantedBy=multi-user.target

Close the editor and save the changes. Then proceed to reload the daemon with the following command:

sudo systemctl daemon-reload

Finally, you can simply start the janus service with the following command:

sudo systemctl start webrtcserver

You can check the status with:

sudo systemctl status webrtcserver

Which should output something like this:

● webrtcserver.service - My Janus WebRTC Server
   Loaded: loaded (/etc/systemd/system/webrtcserver.service; disabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-06-08 20:06:33 -05; 2 weeks 0 days ago
 Main PID: 28681 (sudo)
    Tasks: 33 (limit: 4915)
   CGroup: /system.slice/webrtcserver.service
           ├─28681 /usr/bin/sudo /opt/janus/bin/janus
           └─28682 /opt/janus/bin/janus

You may need as well to enable the service, so it can start everytime the server reboots:

sudo systemctl enable webrtcserver

This will create a symbolic link from the system’s copy of the service file (usually in /lib/systemd/system or /etc/systemd/system) into the location on disk where systemd looks for autostart files (usually /etc/systemd/system/some_target.target.wants.

Happy coding ❤️!

This could interest you

Become a more social person