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

How to publish before shutdown

asked 2018-12-19 02:19:58 -0500

KSWang gravatar image

Hi, everyone. I use rosserial_python to connect arduino in my project. I need to publish Twist information to arduino before I shut ros program down. I've already tried two ways to write shutdown function which is refer from

and here's one of my code:

rospy.loginfo(rospy.get_caller_id() + " shutdown------------------------")

twist.linear.x = forward_init

twist.angular.z = left_init


rospy.loginfo(rospy.get_caller_id() + " finish shutdown-----------------")


However in it shows both of loginfo, but it doesn't publish in this part. Appreciated for any suggestion. thanks a lot.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-12-19 02:29:50 -0500

mgruhler gravatar image

You link to the C++ documentation, but you show python code... How is this supposed to work?

Also, if you want to publish before shutdown, you need to do something entirely different.

In you code, you only publish once at the very beginning. Then you enter spin() which is a blocking call. You need to put the logic in another thread then, or use the while not rospy.is_shutdown(): pattern instead of spin().

To publish a message after shutdown has been triggered, but before it occurs (I understand your question this way), I don't see much of possibilities besides using a shutdown hook. But note that

Messages are not guaranteed to be published.

edit flag offensive delete link more


Hi, thanks for your reply. Sorry, I made a mistake of the website link. I refer this website and find another way for python. And I've used rospy.is_shutdown(), but it still not worked. I think my question is to publish when I shut down the program. Maybe it's fine before or after shutdown.

KSWang gravatar image KSWang  ( 2018-12-19 09:38:45 -0500 )edit

Well, that's the thing. When ROS is shut down, you cannot publish. Thus, your only chance would be to go via the shutdown hootk linked above. But then again,

the message is not guaranteed to be published.


mgruhler gravatar image mgruhler  ( 2018-12-20 01:50:52 -0500 )edit


This [the shutdown hook] will be invoked before actual shutdown occurs, so you can perform service and parameter server calls safely.

Would a service also be fine for you?

mgruhler gravatar image mgruhler  ( 2018-12-20 01:51:13 -0500 )edit

Thanks again, I slightly understand what you mean. but, I can't figure out how service will make it work.

KSWang gravatar image KSWang  ( 2018-12-20 02:08:36 -0500 )edit

Question Tools



Asked: 2018-12-19 02:19:58 -0500

Seen: 1,129 times

Last updated: Dec 19 '18