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

How to exit a ROS node on keyboard interrupt?

asked 2016-07-05 23:46:08 -0500

Cerin gravatar image

How do you write a Python ROS node so that it exits in response to a KeyboardInterrupt exception?

I have a trivial node like:

class MyNode:

    def __init__(self):
        while 1:
            do stuff

if __name__ == '__main__':
    rospy.init_node('mynode')
    MyNode(rospy.get_name())

But pressing ^C doesn't stop it. I see my "^C" interrupt character appear in the output of my "do stuff" code, but the node continues to run, almost as though it's not running in the main thread.

I tried implementing a rospy.on_shutdown() callback, but it's never called in response to a KeyboardInterrupt.

edit retag flag offensive close merge delete

Comments

rospy.on_shutdown() callback has to be called before SIGKILL. Can you please show your implementation?

janindu gravatar image janindu  ( 2016-07-06 00:12:52 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-07-06 00:44:10 -0500

ahendrix gravatar image

The publisher in the rospy publisher and subscriber tutorial demonstrates how to do this: your infinite loop needs to check rospy.is_shutdown():

while not rospy.is_shutdown():
    # do stuff
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-07-05 23:46:08 -0500

Seen: 3,482 times

Last updated: Jul 06 '16