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

Revision history [back]

click to hide/show revision 1
initial version

Could you describe your problem a bit more detailed? Is it really important that the driver-node is killed last or is it important that it not suddenly disappears when another node needs it, e.g. to stop a robot movement? A node is not suddenly terminated as soon as the launch file is closed, as you can see with this small node:

#! /usr/bin/python

import rospy
import signal
from time import sleep

rospy.init_node("killme")

def handler(sig_num, frame):
    print "GOT SIGNAL", sig_num

signal.signal(signal.SIGTERM, handler)

while not rospy.is_shutdown():
    rospy.loginfo("i'm alive")
    rospy.sleep(1)

print "LAUNCH FILE WAS TERMINATED"

i = 0
while True:
    print "i'm still alive", i
    i+=1
    sleep(1)

If you launch this and terminates the launch file, this happens:

core service [/rosout] found
process[killtest-1]: started with pid [9122]
[/killtest INFO 1484400429.490688]: i'm alive
[/killtest INFO 1484400430.491938]: i'm alive
[/killtest INFO 1484400431.493292]: i'm alive
[/killtest INFO 1484400432.494716]: i'm alive
^C[killtest-1] killing on exit
LAUNCH FILE WAS TERMINATED
i'm still alive 0
i'm still alive 1
i'm still alive 2
(...)
i'm still alive 13
i'm still alive 14
[killtest-1] escalating to SIGTERM
GOT SIGNAL 15
i'm still alive 15
i'm still alive 16
i'm still alive 17
[killtest-1] escalating to SIGKILL
Shutdown errors:
 * process[killtest-1, pid 9122]: required SIGKILL. May still be running.
shutting down processing monitor...
... shutting down processing monitor complete
done

Your node first gets a gentle request to terminate (rospy.is_shutdown or ros::ok in C++), afterwards, you have around 15 sconds until ROS is getting a bit more direct by sending a SIGTERM which you still free to ignore, after another 3 seconds, ROS is killing you without mercy.

If the other nodes are terminating nicely after the rospy.is_shutdown, your driver node has 15 to 18 seconds to clean up, spawn other programs or just activate the emergency stop after the SIGTERM.

I know that this does not really answer your specific question but I think it could be usable for a solution to your problem.