#!/usr/bin/env python import rospy from std_msgs.msg import String import rospkg import subprocess import os import signal class Bagger(object): def __init__(self): rospy.init_node("bagger", anonymous=True) rospy.Subscriber("bag_publisher", String, self.callback) self.pub = rospy.Publisher("bag_notifier", String, queue_size=10) self.proc = None rospack = rospkg.RosPack() self.data_path = os.path.join(rospack.get_path("rosbridge_GUI_example"), "data") def callback(self, msg): if msg.data == "STOP" and self.proc is not None: os.killpg(self.proc.pid, signal.SIGINT) self.pub.publish("STOPPED") else: bag_file = os.path.join(self.data_path, msg.data) self.proc = subprocess.Popen(["rosbag", "record", "--all", "-o", bag_file], preexec_fn=os.setsid) self.pub.publish("STARTED") if __name__ == '__main__': Bagger() rospy.spin()