Ask Your Question

Revision history [back]

You can register a callback for the shutdown hook in rospy as described here.

This shutdown even is called before the ros system closes so you should still be able to write log messages. Your code will look something like this:

!/usr/bin/env python
import rospy
from std_msgs.msg import String

def talker():

    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

def my_shutdown_handler():
  print "shutdown time!"
  rospy.loginfo("test shutdown.")

try:
    rospy.on_shutdown(my_shutdown_handler)
    talker()
except rospy.ROSInterruptException:
    pass