Kill rosbag gracefully via kill
Asked Answered
F

4

7

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.

Folia answered 2/6, 2018 at 2:2 Comment(3)
This question has several duplicates on ROS Answers. You can use rosnode kill or take care of killing child processes in your script.Clarisaclarise
oh, that's nice! Would you recommend to close my question, or should I leave it for reference?Folia
I think it could be useful to keep as reference, since there seem to be quite a lot of people not aware of ROS Answers. You can write a brief answer yourself, explaining how you fixed it in the end with a link to the related ROS Answers question.Clarisaclarise
F
10

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

Folia answered 5/6, 2018 at 12:31 Comment(1)
Is there a way to find the output filename that the rosnode with name my_bag is writing to later? I tried rosnode info /my_bag but it doesn't list it.Byzantium
D
0

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
Drawer answered 21/8, 2019 at 15:9 Comment(0)
C
0

I wanted to start rosbag recordings from within a launchfile with

<node pkg="rosbag" type="record" name="rosbag_recorder" args="record --all --exclude '/unwanted/(.*)'"/>

and had the issue of .active-files when stopping roslaunch with CTRL+C.

A solution which works well for me is to start the following node in the launchfile, which triggers rosnode kill upon user request by typing "stop":

#!/usr/bin/env python
import rospy
import subprocess

NODENAME = 'keypress_killer'
TAG = NODENAME + ": "

if __name__ == '__main__':
    rospy.init_node(NODENAME)
    while not rospy.is_shutdown():
        try:
            rospy.logwarn(TAG + "Plase type quoted \"stop\" to gracefully kill all nodes and the rosbag recording")
            input_str = input()
            if input_str == "stop":
                rospy.logwarn(TAG + "Stopping all nodes and rosbag recording...")
                subprocess.check_call(['rosnode', 'kill', '-a'])  # gracefully kill the rosbag recorder
            else:
                raise ValueError
        except:
            rospy.logerr(TAG + "Unknown command, please type quoted \"stop\" to gracefully kill all nodes and the rosbag recording")
            rospy.sleep(0.1)

Cavalry answered 22/10, 2021 at 10:15 Comment(0)
Z
0

Starting rosbag node as giving special name because for shutting down easily, and also if you want give a special name for bagfile name;

-a means save all topics, you can give topic name if you want

rosbag record --output-name=bagFileName -a __name:=bagNodeName

Now you can see a running node by the name of 'bagNodeName' if you run rosnode list and also bagFileName.bag.active file in a relative folder

and then for shutting down node and save bag file automatically;

rosnode kill bagNodeName

Now you should see bagFileName.bag file

Zitazitah answered 14/1, 2022 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.