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

Is it possible to stop or restart a node at run time?

asked 2021-05-26 01:29:06 -0500

first-penguin gravatar image

Suppose you have three nodes A, B, and C running at the same time. Is it possible to stop only A on the way and restart A after a while? I use the ROS melodic. Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-12-17 10:53:29 -0500

lucasw gravatar image

updated 2022-07-10 12:06:35 -0500

Here's a hacky bash script to restart an already running node, though the node name needs to be unique (it doesn't need to be the full node name, it can match on anything unique that comes up in ps -eo args | grep foo for the node process, so it could even be the name of the executable if that is the only instance):

#!/usr/bin/env bash
# source my_partial_node_name

echo $node_name

# Get the command to launch the node, which should have __name 
# and remaps, but args are still on the parameter server so 
# are okay not to be here.
# Only take first match, which may fail in combination
# with the rosnode list below
orig_cmd=`ps -eo args | grep  "__name:=$node_name" | grep __name | head -n 1`
# get the namespace
# (why don't roslaunched nodes have namespaces in the ps output?)
node_path=`rosnode list | grep $node_name$ | sed 's:/[^/]*$::'`
# adding the namespace here may be redundant but is harmless
# if specified twice
cmd="$orig_cmd __ns:=$node_path"
echo "press ctrl-c then up arrow to run this again (only works if running this with source)"
history -s $cmd


Probably there are any number of cases where this fails, or there is a much easier way to do this.

It needs to be run with source for the history modification to work so you can press up arrow to run it again.

I find this useful for after doing a large roslaunch which includes a single node I'm modifying, so make a change then restart the node possibly many times. Or a node just fails unexpectedly (but not so hard it stops executing so there would be nothing for ps to find, e.g. an exception in a callback but the main node process keeps going)- make a fix and restart it quickly.

(Restarting a node with identical settings from the command line with nearly zero effort ought to be a first class node-based robotics middleware feature, maybe a less hacky version of this should be alongside rosrunin ...)

edit flag offensive delete link more

answered 2021-05-26 03:33:24 -0500

Ranjit Kathiriya gravatar image

updated 2021-05-26 06:42:21 -0500

Yes, It is possible. You have to kill the node and You can restart that particular node again.

You can use this command, I think that will work for you.

import os
os.system("roslaunch realsense2_camera rs_camera.launch")    //start
os.system("rosnode kill /camera/realsense2_camera")          //stop

You can also have a look at these links. These ways can be useful:

edit flag offensive delete link more

Question Tools



Asked: 2021-05-26 01:29:06 -0500

Seen: 3,746 times

Last updated: Jul 10 '22