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

How to exit a ROS node on keyboard interrupt?

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

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__':

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


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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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


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

Seen: 3,431 times

Last updated: Jul 06 '16