You could run your rosbag with a Not anonymous node name
rosbag record -o /file/name /topic __name:=my_bag
and then kill it via rosnode kill :
rosnode kill /my_bag
This assures that rosbag stops gracefully.
Originally posted by Wolf with karma: 7555 on 2017-11-09
This answer was ACCEPTED on the original site
Post score: 7
Original comments
Comment by Girmi on 2018-02-16:
The correct command that worked for me (ROS Kinetic) is:
rosbag record -o /file/name /topic __name:=my_bag
rosnode kill /my_bag
Note the double underscore before the name param (see the "Special keys" section at [Remapping Arguments](http://wiki.ros.org/Remapping Arguments))
Comment by gvdhoorn on 2018-02-16:
You're correct. I've just edited the answer by @Wolf.
Comment by stegelid on 2019-08-21:
You can set a trap in your bash script, catching SIGINT and call rosnode kill from there, eg:
#!/bin/bash
trap "rosnode kill /bagger" SIGINT
rosbag record /my_topic __name:=bagger &
roslaunch mypackage launch.launch
Comment by mch on 2021-10-28:\
rosnode kill /my_bag
This assures that rosbag stops gracefully.
That does not seem to stop gracefully though. Rosbag name is *.bag.active not just *.bag