ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Stop Rosbag subprocess

asked 2014-05-09 06:29:14 -0500

rmb209 gravatar image

I am able to start up a rosbag subprocess using this setup. However, I am unable to kill the rosbag from within the python code. I have tried the methods suggested in and various methods suggested on stackoverflow yet to no avail.

Can anyone offer a solution?

    import rospy
    import rosbag
    import subprocess
    import time
    import os
    import signal
    from std_msgs.msg import Int32, String

    def callback(data):
    #   rospy.loginfo(rospy.get_caller_id()+"I heard %s",
        if == "Start Recording":
            rospy.loginfo(rospy.get_caller_id()+"Start Recording!")

            global proc 
            proc = subprocess.Popen(["rosbag", "record", "-a","-o", "Subprocess"], preexec_fn=os.setsid)
        elif == "Stop Recording":
            rospy.loginfo(rospy.get_caller_id()+"STOPPING RECORDING")
            if 'proc' in globals():
                rospy.loginfo(rospy.get_caller_id()+"Killing Rosbag")
                #os.killpg(, signal.SIGTERM)
            rospy.loginfo(rospy.get_caller_id()+"NO BUENO")

    def listener():

        rospy.init_node('listener', anonymous=True)

        rospy.Subscriber("chatter", String, callback)


    if __name__ == '__main__':
edit retag flag offensive close merge delete


How does it fail to kill?

tfoote gravatar image tfoote  ( 2014-05-11 19:54:25 -0500 )edit

It just doesn't kill the rosbag at all - it carries on running.

rmb209 gravatar image rmb209  ( 2014-05-15 00:05:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-05-15 13:08:08 -0500

tfoote gravatar image

Are you sure it doesn't kill?

You need to kill it in a way that allows it to clean up its child processes. If you look at the process tree

tfoote   32651  0.1  0.0  67512 16148 pts/3    S+   16:02   0:00          |   |   |   \_ /usr/bin/python /opt/ros/hydro/bin/rosbag record -a -o Subprocess
tfoote   32654  0.3  0.0 1293848 7508 pts/3    Sl+  16:02   0:00          |   |   |       \_ /opt/ros/hydro/lib/rosbag/record --buffsize 256 --chunksize 768 -o Subprocess --all

rosbag record creates another child process which actually does the recording. If you kill the parent too hard it won't clean up its children. You should probably send a SIGINT not a SIGKILL.

edit flag offensive delete link more



os.killpg(, signal.SIGINT) worked, thanks

spmaniato gravatar image spmaniato  ( 2014-10-28 12:54:12 -0500 )edit

Question Tools


Asked: 2014-05-09 06:29:14 -0500

Seen: 2,313 times

Last updated: May 15 '14