Tik0
Tik0

Reputation: 2697

Kill rosbag gracefully via kill

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.

Upvotes: 7

Views: 8061

Answers (4)

abdullah yıldız
abdullah yıldız

Reputation: 166

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

Upvotes: 0

F1iX
F1iX

Reputation: 1013

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)

Upvotes: 0

simontegelid
simontegelid

Reputation: 1

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

Upvotes: 0

Tik0
Tik0

Reputation: 2697

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

Upvotes: 10

Related Questions