start and stop rosbag within a python script
Hi,
I wrote an Action Server which gets all topics I want to record via the goal. Everything works fine until i want to stop rosbag. With my script i can start rosbag out of my script and i can see the bag-file which is generated. But when i want to kill the rosbag process out of my script, rosbag doesn't stop. It doesn't stop until i stop my whole ActionServer. But the action server should still run after stopping rosbag.
Here is the code I wrote: (the topic_logger.msg includes the goal, an id and an array of topics)
#! /usr/bin/env python
import roslib; roslib.load_manifest('topic_logger')
import rospy
import subprocess
import signal
import actionlib
import topic_logger.msg
class TopicLoggerAction(object):
# create messages that are used to publish feedback/result
_feedback = topic_logger.msg.topicLoggerFeedback()
_result = topic_logger.msg.topicLoggerResult()
def __init__(self, name):
self._action_name = name
self._as = actionlib.SimpleActionServer(self._action_name, topic_logger.msg.TopicLoggerAction, execute_cb = self.execute_cb)
self._as.start()
rospy.loginfo('Server is up')
def execute_cb(self, goal):
if goal.ID == "topicLog":
# decide whether recording should be started or stopped
if goal.command == "start":
#start to record the topics
rospy.loginfo('now the topic recording should start')
args = ""
for i in goal.selectedTopics:
args = args + " " + i
command = "rosbag record" + args
self.p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=True, cwd=dir_save_bagfile)
rospy.loginfo(self.p.pid)
# process = p
# print p.stdout.read()
# check if the goal is preempted
while 1:
if self._as.is_preempt_requested():
rospy.loginfo('Logging is preempted')
self._as.set_preempted()
break
elif goal.command == "stop":
#stop to record the topics
rospy.loginfo('now the topic recording should stop')
#self.p.send_signal(signal.SIGTERM)
rospy.loginfo(self.p.pid)
killcommand = "kill -9 " + str(self.p.pid)
rospy.loginfo(killcommand)
self.k = subprocess.Popen(killcommand, shell=True)
rospy.loginfo("I'm done")
#while 1:
# if self._as.is_preempt_requested():
# rospy.loginfo('Logging is preempted')
# self._as.set_preempted()
# break
else:
rospy.loginfo('goal.command is not valid')
else:
rospy.loginfo('goal.ID is not valid')
if __name__ == '__main__':
rospy.init_node('topicLogger')
dir_save_bagfile = '/home/ker1pal/'
TopicLoggerAction(rospy.get_name())
rospy.spin()
Are there any ideas why I can't stop rosbag out of my script without killing my whole ActionServer ?
Thanks Ralf