ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

Batch rosbag record from python

asked 2015-07-29 16:14:59 -0500

CatherineH gravatar image

I'm trying to batch process bagfiles using rosbag from within python.

My process is - use multiprocess to open two processes - one that calls the roslaunch I want to record output from, and one that calls rosbag record multiple times to capture different sections of the output.

However, this does not work because I can't seem to kill rosbag record nicely from python.

Here's the minimal code to show an example of what I'm trying to do:

#!/usr/bin/env python
from rosbag import rosbag_main
from multiprocessing import Process
import time
import sys
import os
import signal
import rospy

if __name__ == "__main__":
    #if no roscore is initialized, start one
    if not rospy.core.is_initialized():
    print "No rosmaster found, "
    roscore = Process(target = os.system, args = ["roscore"])
    num_run = 0
    roslaunch = Process(target = os.system, args = ["roslaunch testing_scripts elapsed_time.launch"])

    while num_run<3:
    start_bag_time = time.time()
    print "Starting on "+str(num_run)
    write = Process(target = rosbag_main.record_cmd, args = [['-a']])
    #start the rosbag record process
    while time.time()-start_bag_time<5:

    while write.is_alive():
        print("waiting for write to die")

    print("killed write")
    rospy.logwarn("done with "+str(num_run))
    num_run += 1
    #kill the roslaunch process
    if roslaunch.is_alive():
    print "killing publisher"
    #kill the roscore process, if you started it
    if roscore.is_alive():
    print "killing roscore"

elapsed_launch.launch just publishes a string message containing the elapsed time since the node was initiated once every 100 ms.

I would expect that this script would create three bag files, one for each 5 second chunk in the total operation time of the script.

However, I can't seem to kill the rosbag record process nicely. I've tried SIGINT, it doesn't kill the node. SIGKILL allows the script to continue, however, this does not actually kill the node. At the end of the script, if I look at my processes, I have:

cholloway@nb-cholloway:~/workspace/blackwidow$ ps aux | grep record
chollow+  4931  0.4  0.0 1306408 11576 pts/3   Sl   17:04   0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all
chollow+  5026  0.3  0.0 1306408 11332 pts/3   Sl   17:04   0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all
chollow+  5188  0.3  0.0 1306408 11428 pts/3   Sl   17:05   0:00 /opt/ros/indigo/lib/rosbag/record --buffsize 256 --chunksize 768 --all

How can I nicely kill rosbag record? Is there an easier way to achieve this batch processing of rosbags?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-07-29 17:04:31 -0500

updated 2015-07-29 17:04:53 -0500

This doesn't answer your question directly, but if you want bags split up by time, you can use the --split and --duration options of rosbag record.

edit flag offensive delete link more


yeah, unfortunately this is not the answer I'm looking for. Splitting things up by time is just an example for demonstration purposes, I actually want to trigger an end to rosbag record at arbitrary times.

CatherineH gravatar imageCatherineH ( 2015-07-30 07:49:56 -0500 )edit

Your Answer

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

Add Answer

The deadline to submit a proposal to present at ROSCon 2017 is June 25, 2017. Submit yours now!

Question Tools

1 follower


Asked: 2015-07-29 16:14:59 -0500

Seen: 458 times

Last updated: Jul 29 '15