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

stopping rosbag record from script

asked 2020-12-17 04:57:05 -0600

azerila gravatar image

I have a python subprocess that starts recording a topic through rosbag:

    self.ros_bag_recorder = subprocess.Popen(['rosbag', 'record', '-O',
                                             os.path.expanduser('~/some_address),
                                             '/the_topic'],
                                            env=self.venv,
                                            stdout=subprocess.PIPE,  
                                            stderr=sys.stderr
                                             )

which normally I would have executed in a terminal and after pressing ctrl+c the recording was stopped ( the bag file from .bag.active would have changed to .bag). However now that I have started it through suprocess and want to also stop it with an script rather than terminal, commands such as followings haven't worked:

    self.ros_bag_recorder.send_signal(signal.SIGTERM)

or

    self.ros_bag_recorder.send_signal(signal.SIGKILL)

or

    self.ros_bag_recorder.send_signal(signal.SIGINT)

Does anyone know a way to stop the recording similar to when we just press ctrl+c in terminal?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-12-17 08:00:11 -0600

updated 2020-12-17 08:02:00 -0600

As answered to another similar question (https://answers.ros.org/question/2868...), you can kill it by running this:

for proc in psutil.process_iter():
    if "record" in proc.name() and set(command[2:]).issubset(proc.cmdline()):
        proc.send_signal(subprocess.signal.SIGINT)

self.ros_bag_recorder.send_signal(subprocess.signal.SIGINT)
edit flag offensive delete link more

Comments

is 'command' the same as the command that was used as input to Popen?

azerila gravatar image azerila  ( 2020-12-17 08:05:24 -0600 )edit

Yes, exactly.

gstavrinos gravatar image gstavrinos  ( 2020-12-17 08:08:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-17 04:57:05 -0600

Seen: 627 times

Last updated: Dec 17 '20