Kill rosbag gracefully via kill

一曲冷凌霜 提交于 2019-12-10 06:33:11

问题


I'd like to kill a rosbag instance gracefully via terminal. Gracefully means in that case, that the rosbag file doesn't have the suffix .active after kill.

so I do the following from terminal to send the recommended SIGINT to rosbag:

$ rosbag record /some/topic &
$ RPID=$!
$ # do some stuff 
$ kill -2 $RPID

Unfortunately, the bag remains active and it can happen that not everything was stored to the disk. However, if I put rosbag into a launch file, it seems to work:

$ roslaunch rosbag_record.launch &
$ LPID=$!
$ # do some stuff 
$ kill -2 $LPID

Now the rosbag stays intact and it was stored without the active suffix.

Now the interesting question is, what am I doing wrong in the first case. I though that killing a launch file, and in this case killing the roscore, raises a ros::shutdown() which causes a SIGINT in all processes. But the manual way by using kill seems to has a different behavior.


回答1:


Native signal handeling is not well suported and it is always better to use ROS's intended ways of starting and shutting done nodes, so that the API can keep track. To end a node gracefully, we assume that a rosbag node with name my_bag was started:

rosbag record -o /file/name /topic __name:=my_bag

Then, the node kann be gracefully killed using the rosnode kill command and the name of the node:

rosnode kill /my_bag

Link for reference




回答2:


As mentioned in the reference in @Tik0's answer, you can add a trap in bash to catch SIGINT (Ctrl+C) and call rosnode kill from there, eg.:

#!/bin/bash
trap "rosnode kill /bagger" SIGINT
rosbag record /my_topic __name:=bagger &
roslaunch mypackage launch.launch


来源:https://stackoverflow.com/questions/50652887/kill-rosbag-gracefully-via-kill

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!