Ask Your Question
4

Programmatic way to stop roslaunch

asked 2011-07-05 21:55:41 -0500

smerino gravatar image

updated 2016-01-13 10:12:54 -0500

lucasw gravatar image

I saw in the description of roslaunch this: "roslaunch is an important tool that manages the start and stop of ROS nodes" But I am not able to find how to stop a roslaunch. Is there any method to kill all nodes of a roslaunch in one instructions?

Thank You

edit retag flag offensive close merge delete

9 Answers

Sort by ยป oldest newest most voted
3

answered 2011-07-05 22:14:29 -0500

dornhege gravatar image

Ctrl-C works.

edit flag offensive delete link more

Comments

You can also pass the SIGINT to whatever subprocess handle you started it with in code.
tfoote gravatar imagetfoote ( 2011-07-18 08:50:55 -0500 )edit
6

answered 2016-01-13 11:03:55 -0500

the python api exposes a method for stopping a roslaunch

http://wiki.ros.org/roslaunch/API%20U...

edit flag offensive delete link more

Comments

But that is just for a single node? Maybe if that could take a ros package name and .launch file parameter, and then launch and later kill the whole thing and all child nodes like ctrl-c does, that would be great, though @smerino wants to kill a launch file started elsewhere.

lucasw gravatar imagelucasw ( 2016-01-18 11:07:08 -0500 )edit
1

that is just for a single node?

@lucasw Not sure where you got that impression. ROSLaunchParent.shutdown seems to be able to kill the launch process.

130s gravatar image130s ( 2018-09-24 17:22:19 -0500 )edit
1

Probably it was the documentation that was online at that time: http://wiki.ros.org/action/recall/ros... there isn't a mention of launch files. I think the launch file capability existed for some time prior to being documented, then in lunar argument support.

lucasw gravatar imagelucasw ( 2018-09-25 12:07:04 -0500 )edit

legend. Works well from a simple python script.

Gushu gravatar imageGushu ( 2019-05-22 03:14:38 -0500 )edit
4

answered 2011-07-05 22:22:24 -0500

Bemfica gravatar image

"rosnode kill -a"

edit flag offensive delete link more
2

answered 2016-02-19 14:19:17 -0500

jpiramirez gravatar image

My suggestion would be to rely on one or several nodes to take down the whole roslaunch script when they finish executing. Use the required="true" attribute inside a <node> tag, so whenever your node dies or finishes it takes down all of the other nodes in the launch file.

edit flag offensive delete link more

Comments

This looks like a good idea, but may not suit for situation of Start and Stop .launch file.

sonictl gravatar imagesonictl ( 2016-12-05 20:39:13 -0500 )edit
2

answered 2011-07-05 23:15:15 -0500

smerino gravatar image

updated 2016-01-18 11:07:14 -0500

lucasw gravatar image

Thank you for this answers, but for me it is not a solution because it is another node which launch the roslaunch, and of course there are other nodes, so I cannot kill all nodes with kill. Another idea?

edit flag offensive delete link more

Comments

Can you describe what the intention of that is? You might be doing something more complicated than necessary. Anyways, you can still insert a Ctrl-C/SIGINT into a running process/roslaunch via the kill command.
dornhege gravatar imagedornhege ( 2011-07-06 02:42:00 -0500 )edit
@dornhege: How? I see there is a ros::kill(__pid_t __pid, int __sig), but how do you invoke this from within the code. I mean, how do you know the pid_t of the current node?
ubuntuslave gravatar imageubuntuslave ( 2011-07-16 05:14:41 -0500 )edit
For the current node you can just use getpid(). According to the problem of smerino it depends on how you start it.
dornhege gravatar imagedornhege ( 2011-07-16 05:23:53 -0500 )edit
The problem is that I don't know which are the nodes that the laucher will run. So, if I kill the launcher process, the nodes will be killed?
smerino gravatar imagesmerino ( 2011-09-04 23:00:53 -0500 )edit
Yes, sending SIGINT/Ctrl-C should kill roslaunch, which will kill the launched nodes.
dornhege gravatar imagedornhege ( 2011-09-05 01:28:20 -0500 )edit
2

answered 2016-12-08 07:07:39 -0500

updated 2016-12-08 07:33:44 -0500

I recommend you to use the subprocess module to solve this problem. e.g.:

#!/usr/bin/env python

import rospy
import subprocess
import signal

child = subprocess.Popen(["roslaunch","ros_arduino_python","arduino.launch"])
#child.wait() #You can use this line to block the parent process untill the child process finished.
print("parent process")
print(child.poll())

rospy.loginfo('The PID of child: %d', child.pid)
print ("The PID of child:", child.pid)

rospy.sleep(15)

child.send_signal(signal.SIGINT) #You may also use .terminate() method
#child.terminate()

#for more: https://docs.python.org/2/library/subprocess.html
edit flag offensive delete link more

Comments

does it work on ros kinetic as well. Till now i am not able to execute above script successfully on ros kinetic version. it looks like in below code second argument is rospackage and 3rd one is name of launch file.

roslaunch","ros_arduino_python","arduino.launch"]

can-43811 gravatar imagecan-43811 ( 2017-06-22 11:28:30 -0500 )edit
1

answered 2016-01-23 10:12:53 -0500

superjax gravatar image

updated 2016-01-23 10:15:26 -0500

If you're using C++ you can use system("pkill roslaunch") in your node to kill the roslaunch from the system level. I use it for monte carlo simulations with Gazebo).

edit flag offensive delete link more
1

answered 2016-01-20 17:46:08 -0500

lucasw gravatar image

updated 2016-01-22 11:49:31 -0500

Unless the scriptapi actually can run launch files, here is my (somewhat confusing) solution.

  1. Create a node that uses scriptapi to start/stop a single node specified by params. (the scriptapi approach doesn't look like it can run launch files, just nodes- it is just a programmatic rosrun, not roslaunch?)
  2. Create a node that uses roslaunch directly to start a param defined launch file, with a hack to get around the way roslaunch uses sys.argv. The main method blocks so doesn't have a nice start/stop/is_alive interface like scriptapi.

    sys.agv=['roslaunch', 'pkg_name', 'foo.launch', 'arg:=bar']
    import roslaunch
    roslaunch.main(args)
    
  3. Start the second roslaunch raw node within the first scriptapi node, and then if the stop method is used it uses the scriptapi stop method which interrupts the roslaunch, kills all the child nodes as desired.

Maybe there is an idea to allow scriptapi to run launch files, but it isn't currently implemented: https://github.com/ros/ros_comm/blob/...

This question is similar to http://answers.ros.org/question/22309... and http://answers.ros.org/question/20315...

edit flag offensive delete link more

Comments

is subprocess of python an alternative solution for this?

sonictl gravatar imagesonictl ( 2016-12-07 00:00:24 -0500 )edit

Probably, but I'd like to leverage ros tools as much as possible- it seems like they almost support this but for now I'm using the above multi-stage technique.

lucasw gravatar imagelucasw ( 2016-12-07 20:51:25 -0500 )edit
1

It looks like lunar and beyond python roslaunch supports roslaunch arguments http://wiki.ros.org/roslaunch/API%20U... (my hacky solution is a way to get arguments in), in that example parent.stop() would stop the launch.

lucasw gravatar imagelucasw ( 2018-06-19 16:15:41 -0500 )edit
0

answered 2018-07-31 10:34:59 -0500

jubeira gravatar image

If you want to stop it in the command line, you can kill the process like this:

roslaunch [package] [launchfile] &
roslaunch_PID=$!
# Do whatever you need to do here
kill -INT $roslaunch_PID
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2011-07-05 21:55:41 -0500

Seen: 11,833 times

Last updated: Jul 31 '18