Avoid starting multiple node instances with the same type
It's possible by mistake to start the same launch
file that's already been running so that the same nodes get started and messed up. What's a good way to avoid this situation?
We can obviously write a custom script to look for the node names and take actions we want to (I'm posting pseudo code at the bottom). But because roslaunch
doesn't guarantee the order of execution this would work only unreliably.
I'm mainly (still) on ROS Indigo. I have a node that involves certain physical costly setup upon launch, so rebooting the nodes often isn't an option.
Pseudo launch:
<launch>
<arg name="nodename_a" default="nodename_a" />
<node type="node_detect.py" args="$(arg nodename_a)" required="true" respawn="false" /> <!-- Exits if nodename_a found, which kills the entire launch. -->
<node type="node_a.py" name="$(arg nodename_a)" />
:
</launch>
Pseudo script:
if nodename_a in running_nodes:
sys.exit()
Maybe you can write a launch-prefix that checks if the node is running, or if you're in python you may be able to query the master for the node name before you call rospy.init and avoid starting the node if there's another copy already running?
The
launch-prefix
is a nice idea. It could do whatever and decide whether or not to start the actual ROS node.@130s: are you starting things by hand in an actual (deployed) application? I know ROS1 doesn't really have any infrastructure for it, but what you describe is something that cannot happen if you have a system-level coordinator that is in charge or starting and stopping nodes.
Just noticed: is this really about
type
, orname
?Assuming that the process is automated and that you want to launch the
rospy_tutorials talker_listener.launch
, you can check if it's already launched with:ps faux | grep -v grep| grep "rospy_tutorials talker_listener.launch"
If nothing is printed, the the launch file is not running.
I believe @130s is looking for a bit more granular approach. A launch file could include others launch files (ad infinitum) and start tenths of nodes.