Ask Your Question

Kill roscore and all nodes when rosbag play finishes?

asked 2012-01-25 01:35:42 -0500

Jason LaPenta gravatar image


I'm writing some automated test scripts. I'd like to run a launch script with a bag file, which works fine. But when the rosbag play completes I would like roslaunch to exit so my script can load up a new bag file and rerun roslaunch with the test.launch file. Is there an elegant way to cause all ros nodes and roscore to exit when a bag file is finished? I was thinking of making another script to monitor when rosbag finished and then execute a rosnode kill, but I thought there might be a better way.

Thanks, Jason

edit retag flag offensive close merge delete


This is tangential, but you may want to look into using rostest to bring your test fixture up and down using roslaunch files
kwc gravatar image kwc  ( 2012-01-25 04:33:07 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted

answered 2012-01-25 01:53:31 -0500

updated 2015-11-06 10:34:36 -0500

If you're running all your nodes from the same launch file as the bag, just add required="true" to the node tag for rosbag. This will cause the entire roslaunch to die when rosbag terminates.

edit flag offensive delete link more


this appears to work perfectly, thanks
Jason LaPenta gravatar image Jason LaPenta  ( 2012-01-25 02:33:12 -0500 )edit
When you get a good/the best answer please use the checkbox to the left to mark it as answered. I've done so for you this time.
tfoote gravatar image tfoote  ( 2012-02-02 16:11:30 -0500 )edit

typo: required = 'true'

Roger Yau gravatar image Roger Yau  ( 2015-11-06 03:03:01 -0500 )edit

answered 2012-01-25 01:44:33 -0500

I haven't tried this, but perhaps something like this:

rosbag play foo.bag; rosnode kill -a
edit flag offensive delete link more


rosnode kill -a kind of works but sometimes not all nodes get killed

fivef gravatar image fivef  ( 2014-10-22 05:54:44 -0500 )edit

answered 2015-09-30 02:27:54 -0500

fivef gravatar image

If you want to kill everything related to ros just use:

pkill -f ros

But careful this matches on the command lines use to start all user processes. If a not ros related process contains "ros" this will also get killed. A safer way would be to use:

pkill -f /opt/ros
pkill -f /<path to your workspace>
edit flag offensive delete link more

answered 2012-01-25 01:55:57 -0500

DimitriProsser gravatar image

I would personally do this via Bash. I assume that's what you're doing already, but here's how I would do it:


rosbag play my_bag.bag 

while [ ! "$(ps aux | grep bag | grep -v grep)" ] # or $(rosnode list | grep play)
    sleep 1;

if [ "$(ps aux | grep ros | grep -v grep | awk '{print $2}')" ]; then # or $(rosnode list)
    for i in $(ps aux | grep ros | grep -v grep | awk '{print $2}')
        kill -2 $i;

I use this because according to the rosnode kill documentation, it's not guaranteed to actually kill a node, especially when it's set to respawn. This function guarantees that they all die. You'd then be free to launch again. You could even put this all in a loop.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-01-25 01:35:42 -0500

Seen: 13,504 times

Last updated: Nov 06 '15