ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Systemd does not work with ROS2

asked 2020-12-17 07:01:41 -0500

cocodmdr gravatar image

updated 2020-12-17 07:04:12 -0500

Hello,

I am having a hard time working with systemd.
I would like to run some ROS2 node or launch file at startup, but it does not work.
I am using ROS2 Foxy on Ubuntu 20.04.

Here is my systemd unit service file (/etc/systemd/system/talker.service):

[Unit]
Description=run a simple ROS2 script
After=multi-user.target

[Service]
Type=simple
WorkingDirectory=/home/student/
ExecStart=/usr/bin/bash /usr/local/bin/talker.sh

And here is my script (/usr/local/bin/talker.sh):
I added a ' so we can see the hashtag '#

'#!/bin/bash
source /opt/ros/foxy/setup.bash
echo "run simple talker node"
ros2 run demo_nodes_cpp talker

Then I run in a terminal:

sudo chmod 744 /usr/local/bin/talker.sh
sudo chmod 664 /etc/systemd/system/talker.service
sudo systemctl daemon-reload
sudo systemctl enable talker.service
sudo systemctl start talker.service
systemctl status talker.service

And I get this error :

● talker.service - run a simple ROS2 script
Loaded: loaded (/etc/systemd/system/talker.service; static; vendor preset: enabled)
Active: failed (Result: exit-code) since Thu 2020-12-17 13:10:41 CET; 1s ago
Process: 16553 ExecStart=/usr/bin/bash /usr/local/bin/talker.sh (code=exited, status=250)
Main PID: 16553 (code=exited, status=250)

Dec 17 13:10:40 student-W65-W67RZ1 systemd[1]: Started run a simple ROS2 script.
Dec 17 13:10:40 student-W65-W67RZ1 bash[16553]: run simple talker node
Dec 17 13:10:40 student-W65-W67RZ1 bash[16592]: [ERROR] [1608207040.834854884] [rclcpp]: rcl context unexpectedly not shutdown during cleanup
Dec 17 13:10:40 student-W65-W67RZ1 bash[16592]: terminate called after throwing an instance of 'rclcpp::exceptions::RCLError'
Dec 17 13:10:40 student-W65-W67RZ1 bash[16592]: what(): failed to configure logging: Failed to get users home directory, at /tmp/binarydeb/ros-foxy-rcl-logging-spdlog-1.0.1/src/rcl_logging_spdlog.cpp>
Dec 17 13:10:41 student-W65-W67RZ1 systemd[1]: talker.service: Main process exited, code=exited, status=250/n/a
Dec 17 13:10:41 student-W65-W67RZ1 systemd[1]: talker.service: Failed with result 'exit-code'.

If someone can share a systemd unit service file to make it work or explain the error it would be greatly appreciated.
Or if someone recommands a better way of starting files at startup.

Regards,
Cocodmdr

edit retag flag offensive close merge delete

Comments

In one of the last lines it says it failed to get the home directory for the user. Typically those scripts are not executed as your "normal" use but as a system user. That user might not actually have a home directory set. You probaply need to add another system user with a home directory or tell systemd to launch the script with your user

Simon Schmeisser gravatar image Simon Schmeisser  ( 2020-12-18 15:52:01 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2021-11-27 18:26:13 -0500

zhummming gravatar image

I encounter the same question, and i solve it by setting HOME environment in the service file:

Environment="HOME=root"

add this line in [Service], and ros2 will work fine in systemctl.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2020-12-17 07:01:41 -0500

Seen: 2,555 times

Last updated: Dec 17 '20