Stop Rosbag subprocess
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()
How does it fail to kill?
It just doesn't kill the rosbag at all - it carries on running.