Run rtpmidi as a service on Raspberry Pi

A service is a program that the operating system automatically starts when it boots. On the Raspberry Pi “buster” operating system, the daemon that starts and stops services is called “systemd.” You can read about creating services here:

https://www.raspberrypi.org/documentation/linux/usage/systemd.md

McLaren Labs rtpmidi can be run as a service so that whenever you start your Raspberry Pi rtpmidi can be ready to go. This is especially useful in a headless system where you want rtpmidi to route incoming RTP MIDI sessions to a specific MIDI destination.

One of our customers wanted to know how to do this, so we wrote up this HOWTO guide. Here we will show how to create a service that

  • listens for incoming RTP-MIDI connections
  • connects to MIDI Alsa port 128
  • announces itslef on UDP Port 5006
  • logs its output to Syslog

What is syslog?

Syslog is the system logger. It is where services of all sort send their logging messages. If you have never looked at it on your Raspberry Pi, you might find it interesting. Try it out.

$ tail -f /var/log/syslog

The nice thing about syslog is that it is automatically rotated. If you run a program that needs to log messages and it continuously logs to the same file, the file can get too big and can cause your computer to run out of disk space. Syslog is automatically broken into chunks and rotated. The oldest chunks are deleted automatically. It’s good to use syslog for long-running services because they can generate an unbounded number of log messages.

rtpmidi has a means to send its log messages to syslog (which unfortunately, is not documented very well). The use of the option is shown below.

$ rtpmidi -- -GSLogSyslog yes

This ability is provided by GNUStep which can reroute the standard logging function (NSLog) to be sent to syslog. The special option after the double-hyphen turns this capability on for rtpmidi. Note: This needs to be after all of the other options to rtpmidi.

Create a service file

A service file configures a program to be run as a service by systemd. Create a file called “rtpmidi-listen.service” with the contents shown below.

[Unit]
Description=RTPMIDI Listen and forward to MIDI Client 128
After=network.target

[Service]
ExecStart=/opt/rtpmidi_0.5.2-buster/bin/rtpmidi listen -u 5006 -p 128:0 -- -GSLogSyslog YES
WorkingDirectory=/home/pi/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Now copy this file into /etc/systemd/system as root. Type these command to register the service with systemd.

$ sudo systemctl daemon-reload

After this you will be able to start the service with

$ sudo systemctl start rtpmidi-listen.service

To make the service survive a reboot and automatically restart, use the “enable” command.

$ sudo systemctl enable rtpmidi-listen.service

The RaspberryPi will advertise itself as a Bonjour MIDI service on port 5006. You should then be able to initiate a connection to the RPi from another computer, and it will send its MIDI information to MIDI client 128.

You can see log messages in Syslog. Have a look here.

$ tail -f /var/log/syslog

1 thought on “Run rtpmidi as a service on Raspberry Pi”

  1. Francois Malnovic

    Hello!
    I’d like to buy rtpmidi but I don’t know what version I have to choose for the last official Raspberry pi OS?

    May you help me on this?
    Kind regards,
    François Malnovic.

Leave a Reply to Francois Malnovic Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.