Ask Your Question

Revision history [back]

This answer is in Python but you could use something for Matlab. The source of the code:

#!/usr/bin/env python

import rospy
import subprocess
import os
import signal

class RosbagRecord:
    def __init__(self):

        if rospy.has_param('~record_script') and rospy.has_param('~record_folder'):
            self.record_script = rospy.get_param('~record_script')
            self.record_folder = rospy.get_param('~record_folder')

            # Start recording.
            command = "source " + self.record_script
            self.p = subprocess.Popen(command, stdin=subprocess.PIPE, shell=True, cwd=self.record_folder,

            # Wait for shutdown signal to close rosbag record
            rospy.signal_shutdown(rospy.get_name() + ' no record script or folder specified.')

    def terminate_ros_node(self, s):
        # Adapted from
        list_cmd = subprocess.Popen("rosnode list", shell=True, stdout=subprocess.PIPE)
        list_output =
        retcode = list_cmd.wait()
        assert retcode == 0, "List command returned %d" % retcode
        for str in list_output.split("\n"):
            if (str.startswith(s)):
                os.system("rosnode kill " + str)

    def stop_recording_handler(self):
        rospy.loginfo(rospy.get_name() + ' stop recording.')

if __name__ == '__main__':
    rospy.loginfo(rospy.get_name() + ' start')

    # Go to class functions that do all the heavy lifting. Do error checking.
        rosbag_record = RosbagRecord()
    except rospy.ROSInterruptException:

It can be used as ROS node, so once you reach the goal, then you can publish to stop recording. I searched in prior answers and couldn't find something more concise to do this. Hope this helps.