Shutdown C++ node properly

asked 2020-05-09 19:01:33 -0600

Mickael gravatar image


I am trying to shutdown my C++ node properly using the code provided here :

#include <ros/ros.h>
#include <signal.h>

void mySigintHandler(int sig)
  // Do some custom action.
  // For example, publish a stop message to some other nodes.

  // All the default sigint handler does is call shutdown()

int main(int argc, char** argv)
  ros::init(argc, argv, "my_node_name", ros::init_options::NoSigintHandler);
  ros::NodeHandle nh;

  // Override the default ros sigint handler.
  // This must be set after the first NodeHandle is created.
  signal(SIGINT, mySigintHandler);

  return 0;

This is working nicely when I stop my node using CTRL+C, but it is not working when I kill the node using rosnode kill node_name. Is there any way to do some last actions before the node stops when it is killed?



edit retag flag offensive close merge delete