想在Python代码中关闭roscore,所以想找到roscore的进程号,然后再kill。
针对Python2
import commands
if __name__ == '__main__' :
p = commands.getoutput("pgrep roscore ")
commands.getoutput("kill %s " % p)
pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行,它还有一些参数,如-l,-n,需要细究的可以查下。
但是在Python中,command模块被subprocess取代 ,所以此时代码应为:
import subprocess
if __name__ == '__main__' :
p = subprocess.getoutput("pgrep roscore ")
subprocess.getoutput("kill %s " % p)