Pergunta

I am writing a python program that runs the following:

import subprocess
import time


def fun1():
    terminal1 = ['gnome-terminal']
    terminal1.extend(['-x', 'sh', '-c', '"roscore"'])
    pid = subprocess.Popen(terminal1, stdout=subprocess.PIPE)
    time.sleep(3)
    print "success1"
    fun2()





def fun2():
    terminal2 = ['gnome-terminal']
    terminal2.extend(['-x', 'sh', '-c', '"rosrun rosserial_python serial_node.py /dev/ttyACM0"' ])

    pid2 = subprocess.Popen(terminal2, stdout=subprocess.PIPE)
    print "success2"

fun1()

fun1 works properly, I wait 3 seconds because it lasts sometime until everything is done so that the fun2 can work (I can't launch both simultaneously, fun2 has to wait to fun1, which never ends before fun2)

the problem comes when running fun2, I don't know where is the mistake, is the "same" code as in fun1, but the gnome-terminal just appears for few milliseconds and then it disappears...

any suggestion??

thank you in advance

Foi útil?

Solução

The issue is probably with the ". You don't need to use these when passing a list of params to subprocess. It will be escaped properly before being run.

The window is closing because the generated command is probably malformed and thus generates an error and exits immediately.

So if you have the literal command you want to run, you can use shlex.split to generate the appropriate list to pass to subprocess. Usually, you just don't have to worry about quotes and escape characters. So to pass a space as an argument, just write a space.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top