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

Revision history [back]

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 $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

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 the 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 rosrun in https://github.com/ros/ros/tree/noetic-devel/tools/rosbash/scripts ...)

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 $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

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 the 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 rosrun in https://github.com/ros/ros/tree/noetic-devel/tools/rosbash/scripts ...)

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
# args are still on the parameter server so 
# are okay not to be here.
# Only take first match, which may fail in combination combination
# with the rosnode list below
orig_cmd=`ps -eo args | grep $node_name | grep __name | head -n 1`
# get the namespace
# (why don't roslaunched nodes have namespaces in the ps 
# s output?)
node_path=`rosnode list | grep $node_name | sed 's:/[^/]*$::'`
# adding the namespace here may be redundant but is harmless 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

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 rosrun in https://github.com/ros/ros/tree/noetic-devel/tools/rosbash/scripts ...)

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 $node_name  "__name:=$node_name" | grep __name | head -n 1`
# get the namespace
# (why don't roslaunched nodes have namespaces in the 
# s ps output?)
node_path=`rosnode list | grep $node_name $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

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 rosrun in https://github.com/ros/ros/tree/noetic-devel/tools/rosbash/scripts ...)

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/292dc8d7dc82d6c4e99350498e173181)

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 rosrun in https://github.com/ros/ros/tree/noetic-devel/tools/rosbash/scripts ...)