Using Raspberry PI to control Märklin model railway – Part 2

At this point, the CAN interface has to be initialized manually after every reboot. Therefore, if the Pi is used as a permanent controller, it might be preferable to have these initialization steps been carried out during startup.

In order to achieve that, can2upd is going to be registered as a service and started right after the can0 interface has been initialized. Raspbian is using systemd as service manager, so this post will describe how to create systemd units for the tasks described above. Information about how systemd works and how to create unit configuration files can be found in the systemd(1) Linux manual page or in Justin Ellingwood’s excellent article on DigitalOcean .

The CAN interface initialization service

Create a new unit configuration file using sudo vim /lib/systemd/system/socketcan-interface.service with the following contents.

The can0 interface should be initialized as soon as networking is available, which should be the case on runlevel 3 that translates to for systemd. Since all commands used in this unit will exit after completion, the type has to be set to oneshot along with the RemainAfterExit= option set to yes. Multiple commands have to be separated with ;. Executing

will set permissions and enable the unit.

The can2udp service

After the can0 interface has been setup, can2udp has to be started. Since it is desirable to collect as much information as possible about the system’s state, can2udp will be run in foreground mode, allowing the verbose option to be activated. The output can be retrieved later using journalctl, see the troubleshooting section for more information. Create a new unit configuration file using sudo vim /lib/systemd/system/can2udp.service with the following contents.

Putting socketcan-interface.service in both the After= and Requires= section will make this service run after socketcan-interface.service has been started and will attempt to start it in case it’s not running. Executing

will set permissions and enable the unit.


After both systemd units have been created and enabled, the Raspberry PI has to be rebooted. From this point, whenever the PI starts, the can0 interface will be up and running with a proper baudrate and can2udp will be started waiting for incoming UDP connections on its default port.


An overview of all running systemd units can be found using sudo systemctl list-units. Both the socketcan-interface.service and the can2udp.service unit should be listed as loaded and active. Since the socketcan-interface is of type oneshot, it will be marked as exited while can2udp should be indicated as running.

Log output of the respective units can be viewed using sudo journalctl -u can2udp or sudo journalctl -u socketcan-interface. Linux will buffer the output written by can2udp and not write it to /var/log/ per line. Because of that, no output of can2udp might appear in the logs at first. Sending a few commands will cause the logging buffer to flush and the logs to appear in the journalctl output.


J. Ellingwood, “Understanding Systemd Units and Unit Files,” DigitalOcean, 17-Feb-2015. [Online]. Available: [Accessed: 06-Aug-2016]
“systemd on 15.04 won’t log stdout of unit - Ask Ubuntu,” 06-May-2015. [Online]. Available: [Accessed: 06-Aug-2016]
Liraz Siri, “Unix buffering delays output to stdout, ruins your day | TurnKey GNU/Linux Blog,” 06-Oct-2014. [Online]. Available: [Accessed: 06-Aug-2016]
“systemd(1) - Linux manual page,” 2016. [Online]. Available: [Accessed: 06-Aug-2016]