[Deployment] Creating ROS 2 Linux services - cpslabor-education/robotwiki GitHub Wiki
Creating ROS 2 Linux services
It's a typical requirement that for an embedded system, that it starts the required services and no further action is needed for operation and maintenance. In ROS 1, this task tended to be tedious, many wrapper packages has been developed for automatic start and maintenance.
Fortunately, ROS 2 is specifically designed for production-level embedded systems and blends well with other system-level services and applications.
Creating ROS 2-based system service with systemd
It is possible to create a conventional Linux service for an ROS 2-based application. Just keep in mind: the environment variables and paths must be set explicitly. Also, ROS 2 tasks (run, launch, etc.) can be accessed on a direct path. For example, on my BeagleBone, I start SICK S300 as a systemd service, with the following configuration file (ros2 run is used in this example):
[Unit]
Description=ROS 2 SICK S300 service
[Service]
User=debian
Environment="ROS_DOMAIN_ID=0"
Environment="LD_LIBRARY_PATH=/opt/ros/robotom/lib:/opt/ros/foxy/lib"
Environment="PYTHONPATH=/opt/ros/robotom/lib/python3.7/site-packages:/opt/ros/foxy/lib/python3.7/site-packages/"
Environment="AMENT_PREFIX_PATH=/opt/ros/robotom:/opt/ros/foxy"
ExecStart=/opt/ros/foxy/bin/ros2 run sick_s300_ros2 sick_s300_driver_node --ros-args -r /scan:=/robotkocsi0/scan --params-file /opt/ros/robotom/share/sick_s300_driver/configuration/sick_s300.yaml
Restart=on-failure
[Install]
WantedBy=multi-user.target
Alias=laserscanner.service
NOTE: always check your system Python version (Python 3.7 or 3.8).
Keep in mind, that these services should be placed in /lib/systemd/system/ folder. From further on, the systemd commands could be used.
As a reminder, to enable a systemd service:
sudo systemctl enable sicks300.service
To start a systemctl service:
sudo systemctl start sicks300.service
To stop a systemctl service:
sudo systemctl stop sicks300.service
To view the status of the SICK s300 service:
sudo systemctl status sicks300.service
To view the journal (the log) of each associated service:
sudo journalctl -u sicks300.service