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

killing a rosrun process using python script

asked 2015-11-17 09:52:50 -0600

RSA_kustar gravatar image

Hi, I am writing a GUI interface to run nodes in ROS. The following code execute a rosrun process when pressing Button1. I looked for a code in order to kill that process when pressing Button 2. I did not find something specific or related to this work.

#simple GUI

import os, time, signal, threading
from Tkinter import * 
from subprocess import call
from subprocess import Popen  
import subprocess
import numpy as np
import cv2
#from PIL import Image, ImageTk
from PIL import *

def f1():
    subprocess.Popen(["rosrun", "package_name", "nodename"])
def f2():

button1 = Button(root,text="CORE", bg="black", fg="white",  width=25, padx = 20, command=f1)
button2 = Button(root,text="CORE", bg="black", fg="white",  width=25, padx = 20, command=f2)
edit retag flag offensive close merge delete


Have you tried just killing the opened process with normal functionality from the subprocess module?

dornhege gravatar image dornhege  ( 2015-11-17 09:58:13 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-11-17 10:10:24 -0600

The subprocess.Popen instantiator inside of your f1() function returns a subprocess.Popen instance. That object has a kill method that allows you to terminate subprocesses. A simple re-write could be something like

proc = None
def f1():
    global proc
    proc = subprocess.Popen(["rosrun", "package_name", "nodename"])
def f2():

Alternatively, if you are going to be spawning ROS nodes, you could use rosnode.kill_nodes to shutdown nodes that you have started.

edit flag offensive delete link more


Thank you :) it worked perfectly

RSA_kustar gravatar image RSA_kustar  ( 2015-11-28 04:28:36 -0600 )edit

Question Tools

1 follower


Asked: 2015-11-17 09:52:50 -0600

Seen: 4,396 times

Last updated: Nov 17 '15