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

Stop Rosbag subprocess

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

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 http://answers.ros.org/question/10714... 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",data.data)
        if data.data == "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 data.data == "Stop Recording":
            rospy.loginfo(rospy.get_caller_id()+"STOPPING RECORDING")
            if 'proc' in globals():
                rospy.loginfo(rospy.get_caller_id()+"Killing Rosbag")
                proc.terminate()
                #os.killpg(proc.pid, signal.SIGTERM)
                #proc.send_signal(subprocess.signal.SIGINT)
        else:
            rospy.loginfo(rospy.get_caller_id()+"NO BUENO")

    def listener():

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

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


        rospy.spin()

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

Comments

How does it fail to kill?

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

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

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

1 Answer

Sort by ยป oldest newest most voted
2

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

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

Comments

1

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

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

Question Tools

Stats

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

Seen: 2,383 times

Last updated: May 15 '14