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

Proper way to shutdown /rosout (roscore).

asked 2017-06-05 07:45:07 -0600

JulianR gravatar image


I have a project where i start gazebo and several other rosnodes.

I want to be able to shut them all down at some point. I have a node that waits for a certain message and then shuts all other nodes down following with a self shutdown call like:

system("rosnode kill NAME_OF_NODE"); // shutdown other nodes
ros::shutdown();                     // shutdown this node

This does work for everything except the /rosout node. The /rosout node always respawns if killed with rosnode kill. What I tried is to start a roscore in the starting programm before the other roslaunch files are executed, and then at the end, when all nodes finished cleanly I kill the roscore with kill all like:

system("roscore &");                 // start roscore 
...                                  // starting other nodes with roslaunch
system("killall roscore");           // kill roscore

But this does seem like a dirty way. Is there a better way to do this?

edit retag flag offensive close merge delete


3 Answers

Sort by ยป oldest newest most voted

answered 2017-06-07 13:58:01 -0600

Dale McConachie gravatar image

updated 2017-06-07 13:58:38 -0600

My solution is to use a launch file to start all the relevant nodes, with the "terminator" node set to required="true". When the terminator node exits, it will trigger all of the other nodes to close if they are using things like ros::ok() to determine if they should keep running. This only works when roscore has been started by this launch file.

edit flag offensive delete link more


This does work fine, There still is a red message informing about the Initiating of the shutdown, but I guess that is just how it is.

JulianR gravatar image JulianR  ( 2017-06-08 08:58:39 -0600 )edit

answered 2018-01-05 10:51:01 -0600

Cerin gravatar image

I manage ROS like this. I put all launch files inside one master "all.launch", and then I start that using the supervisor service, a simple tool for creating daemons. You can install this on Ubuntu with:

sudo apt-get install supervisor

I configure it by creating a file /etc/supervisor/conf.d/myrobot.conf which looks like:

command=bash -c "source /usr/local/myrobot/src/ros/setup.bash; roslaunch myrobot all.launch"

Then, when the system boots, it launches all your ROS nodes automatically. If you want to cleanly stop it, you'd run:

sudo supervisorctl stop myrobot

Similarly, to start it, you'd run:

sudo supervisorctl start myrobot
edit flag offensive delete link more

answered 2017-06-07 14:05:00 -0600

Airuno2L gravatar image

Start everything from a launch file and everything will shutdown when you hit Ctrl+C on the terminal window that started the launch file.

edit flag offensive delete link more


You could also potentially emulate this by sending SIGINT to all relevant processes. The difference here is that sending SIGINT to the roslaunch process will pass the SIGINT along to all child processes, but will then escalate the signal all the way to SIGTERM if the process does close itself.

Dale McConachie gravatar image Dale McConachie  ( 2017-06-07 14:12:04 -0600 )edit

I don't understand. I've never had something not close when I Ctrl+C a launch file, what would cause something to not close from that?

Airuno2L gravatar image Airuno2L  ( 2017-06-08 09:06:09 -0600 )edit

A couple of straightforward possibilities come to mind:

  • The program has installed its own SIGINT handler instead of using the one setup by ros::init
  • The program has a while(true){ do stuff without checking ros:ok() }

Dale McConachie gravatar image Dale McConachie  ( 2017-06-08 10:39:16 -0600 )edit

Ok, thanks. I wonder if there is a way to make everything go straight to SIGKILL immediately after a Ctrl+C. I have a large launch file and some of the nodes are not ROS code so they take a while to close because it goes from SIGINT -> SIGTERM -> SIGKILL, which takes an annoying amount of time.

Airuno2L gravatar image Airuno2L  ( 2017-06-08 11:42:18 -0600 )edit

I'm not aware of any way to do that via roslaunch, but there could be one. If you have direct access to the code for these other nodes, you could install SIGINT handlers in them.

Dale McConachie gravatar image Dale McConachie  ( 2017-06-08 12:42:14 -0600 )edit

This won't work if in a production environment where you've launched it from a daemon. You really should not be be running production code by manually launching it in a terminal.

Cerin gravatar image Cerin  ( 2018-01-05 10:37:32 -0600 )edit

Question Tools



Asked: 2017-06-05 07:45:07 -0600

Seen: 11,416 times

Last updated: Jan 05 '18