Mr Squid
Mr Squid

Reputation: 1286

Automatically terminate all nodes after calling roslaunch

I am trying to run several roslaunch files, one after the other, from a bash script. However, when the nodes complete execution, they hang with the message:

[grem_node-1] process has finished cleanly
log file: /home/user/.ros/log/956b5e54-75f5-11e9-94f8-a08cfdc04927/grem_node-1*.log

Then I need to Ctrl-C to get killing on exit for all of the nodes launched from the launch file. Is there some way of causing nodes to automatically kill themselves on exit? Because at the moment I need to Ctrl-C every time a node terminates.

My bash script looks like this, by the way:

python /home/user/git/segmentation_plots/scripts/generate_grem_launch.py /home/user/Data2/Coco 0 /home/user/git/Async_CNN/config.txt
source ~/setupgremsim.sh
roslaunch grem_ros grem.launch config:=/home/user/git/Async_CNN/config.txt
source /home/user/catkin_ws/devel/setup.bash
roslaunch rpg_async_cnn_generator conf_coco.launch

The script setupgremsim.sh sources another catkin workspace.

Many thanks!

Upvotes: 2

Views: 10072

Answers (2)

Dr Yuan Shenghai
Dr Yuan Shenghai

Reputation: 1915

Try to do this:

  1. For each launch you put in a separate shell script. So you have N script In each script, call the launch file in xterm: xterm -e "roslaunch yourfacnylauncher"

  2. Prepare a master script which calling all N child script in the sequence you want it to be and delay you want it to have.

Once it is done, xterm should kill itself.

Edit. You can manually kill one if you know its gonna hang. E.g. below:

#!/bin/sh
source /opt/ros/kinetic/setup.bash
source ~/catkin_ws/devel/setup.bash

Start ROScore using systemd or rc.local using lxtermal or other terminals to avoid accident kill. Then run the part which you think gonna hang or create a problem. Echo->action if necessary.

xterm -geometry 80x36+0+0 -e "echo 'uav' | sudo -S dnsmasq -C /dev/null -kd -F 10.5.5.50,10.5.5.100 -i enp59s0 --bind-dynamic" & sleep 15

Stupid OUSTER LIDAR cant auto config like Veloydne and will hang here. Other code can't run.

killall xterm & sleep 1

Let's just kill it and continue to run other launches.

xterm -e "roslaunch '/home/uav/catkin_ws/src/ouster_driver_1.12.0/ouster_ros/os1.launch' os1_hostname:=os1-991907000715.local os1_udp_dest:=10.5.5.1" 

Upvotes: 0

Mr Squid
Mr Squid

Reputation: 1286

Thanks all for your advice. What I ended up doing was this; I launched my ROS Nodes from separate python scripts, which I then called from the bash script. In python you are able to terminate child processes with shutdown. So to provide an example for anyone else with this issue:

bash script:

#!/bin/bash
for i in {0..100}
do
   echo "========================================================\n"
   echo "This is the $i th run\n"
   echo "========================================================\n"
   source /home/timo/catkin_ws/devel/setup.bash
   python planar_launch_generator.py
done

and then inside planar_launch_generator.py:

import roslaunch
import rospy

process_generate_running = True

class ProcessListener(roslaunch.pmon.ProcessListener):
    global process_generate_running

    def process_died(self, name, exit_code):
        global process_generate_running
        process_generate_running = False
        rospy.logwarn("%s died with code %s", name, exit_code)


def init_launch(launchfile, process_listener):
    uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
    roslaunch.configure_logging(uuid)
    launch = roslaunch.parent.ROSLaunchParent(
        uuid,
        [launchfile],
        process_listeners=[process_listener],
    )
    return launch


rospy.init_node("async_cnn_generator")
launch_file = "/home/user/catkin_ws/src/async_cnn_generator/launch/conf_coco.launch"
launch = init_launch(launch_file, ProcessListener())
launch.start()

while process_generate_running:
        rospy.sleep(0.05)

launch.shutdown()

Using this method you could source any number of different catkin workspaces and launch any number of launchfiles.

Upvotes: 1

Related Questions