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

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

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

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
1

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

lucasw gravatar image

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

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 rosrerun.sh my_partial_node_name

node_name=$1
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
$cmd

(https://gist.github.com/lucasw/292dc8...)

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 https://github.com/ros/ros/tree/noeti... ...)

edit flag offensive delete link more
0

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

Ranjit Kathiriya gravatar image

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

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:

https://answers.ros.org/question/2031...

https://answers.ros.org/question/2210...

https://answers.ros.org/question/2230...

edit flag offensive delete link more

Question Tools

3 followers

Stats

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

Seen: 4,577 times

Last updated: Jul 10 '22