Generic answer:
Simply put respawn="true"
in the node parameter list and modify other nodes to handle the offending node's startup and shutdowns. (Unless you are benchmarking, as the OP is. In that case read on)
Specific answer to this situation:
To check for termination of a particular node in a launch file, add required="true"
in the node parameter list as per this document. In short,
<node name="listener1" pkg="rospy_tutorials" type="listener.py" args="--test" required="true" />
This would kill all the nodes launched by that launch file if that required node ends. This solves problem 1 but gives rise to another subtle problem. If you launched another node (say B) not by that launch file, and roscore
was not running prior to launching that launch file, then the node B might not be able to communicate. So you might want to run roscore
before the launch file.
In order to do this again and again, you can use a bash script.
while True; do roslaunch pkg launc_file.launch; done
To kill all the running nodes, you can run
rosnode kill -a
. To kill a particular node, you can runrosnode kill node_name
Just add the required="true" option to one of the <node> tags in the launch file. e.g.
<node ns="stereo" pkg="uvc_camera" type="tara_node" name="uvc_camera" output="screen" required="true">
Akhilesh, so basically if one of my node crashes, then the whole ROS will shutdown? Can I somehow log this or check if the ROS was killed in bash?
hi @l4ncelot, since, right now, I am not working on ROS, so I am not very sure about log, but yes, you can check if the ROS is killed or not using
ps aux|grep <what_you_want_to_check>
Y, that's another way to do it. I found out that I could do also something like
topic_list="$(rostopic list)"
, which is empty string if the ROS is not running anymore.yeah... that's a good way to do. if think my answer is helpful, upvote it.