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

How can I kill a ros2 node with rclpy?

asked 2021-09-16 06:42:40 -0500

FlyingChickenGang gravatar image

Hi, I have a launch file that launches 3 ROS2 nodes and the nodes are wrote using rclpy. Now, I'm planning to write a service call that will kill 2 of those nodes. After doing some research, I found something called "lifecycle Node", but as far as I know "lifecycle" still only supports C++ (according to the outlook

I did found a hacky way ( but I'm not confident with that approach.

So, my questions are: 1) Is there any other way I can kill the ROS2 node other than using pressing ctrl+c and lifecycle node? 2) How can I do the killing of a node properly in rclpy?

I'm using OS=Linux 20.04 LTS | ROS_DISTRO=Foxy | ROS_PYTHON_VERSION=3

edit retag flag offensive close merge delete


Please specify how to use os to find the node's names and PID numbers?

FA gravatar image FA  ( 2023-06-22 03:30:34 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-09-19 10:50:38 -0500

flo gravatar image

You are correct, LifeCycle Nodes are only cpp and would be a nicer implementation. The node in this case would never be completely killed but you can easily restart a node with decent tools.

If you just want to kill the node through a service call, I would recommend to do it with the normal python way: exit(). Than your node should be gracefully exited which has the same effect than what you might desire.

Restarting would then be a part that might be a little bit harder. But probably you could hack your way around with using parts of a launch file in a normal python script. But I would not straight forward recommend that, as I do not know the possible side effects of that.


edit flag offensive delete link more



Thanks for the comment and suggestion. I have also tried another method, which is using python os to find out all the nodes' name and their PID numbers. Then, killing those by using os.kill((PID), signal.SIGTERM).

FlyingChickenGang gravatar image FlyingChickenGang  ( 2021-09-22 03:26:20 -0500 )edit

please consider adding this comment as own answer and tick it as the correct one. Or use mine to show others that this problem can be solved.. :)

flo gravatar image flo  ( 2021-09-23 15:03:32 -0500 )edit

Question Tools



Asked: 2021-09-16 06:42:40 -0500

Seen: 2,257 times

Last updated: Sep 19 '21