0

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

flipmurry
  • 15
  • 1
  • 5
  • Does fun1 window still works after fun2 closes? – 4d4c May 03 '13 at 09:39
  • Yes. Both fun1 window and fun2 window should remain opened because after them, I need a 3rd window to do other commands. Both of them are processes that are running to listen what that 3rd process asks for. I don't know why fun2 window closes... – flipmurry May 03 '13 at 09:44
  • Are you sure the command 'gnome-terminal -x sh -c "rosrun rosserial_python serial_node.py /dev/ttyACM0"' works? If it doesn't work, it'll print an error message, and the terminal will close after that. – Aran-Fey May 03 '13 at 09:58
  • @Rawing The double quotes shouldn't be there, so the command that is actually being run is: `gnome-terminal -x sh -c "\"rosrun rosserial_python serial_node.py /dev/ttyACM0\""` which is invalid, and thus the error and the window closing. – jadkik94 May 03 '13 at 10:01

1 Answers1

0

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.

jadkik94
  • 7,000
  • 2
  • 30
  • 39
  • ton1c, I don't understand... the command I send to gnome-terminal has to be literally like this: (for fun1): >>gnome-terminal --tab -x sh -c "roscore" (for fun2): >>gnome-terminal --tab -x sh -c "rosrun rosserial_python serial_node.py /dev/ttyACM0" if I run them directly in a gnome-terminal, both of them work perfectly, the windows don't close and they stay expecting other things I have to pass them...as it has to be!! but the problem is with the python program, don't know why window from fun2 closes... :( – flipmurry May 03 '13 at 09:57
  • No you did not understand what I mean. Try this: `print subprocess.list2cmdline(['gnome-terminal', '-x', 'sh', '-c', '"roscore"'])` then copy paste that to the terminal emulator, it will not work. Remove the double quotes and it will. – jadkik94 May 03 '13 at 09:58
  • @jadkik94 sorry again, I did what you said and it works, sorry for my newbieness :( you guys are great!! – flipmurry May 03 '13 at 10:03
  • @flipmurry I understand, it is completely correct. Just replace `'"rosrun rosserial_python serial_node.py /dev/ttyACM0"'` with `'rosrun rosserial_python serial_node.py /dev/ttyACM0'` – jadkik94 May 03 '13 at 10:03
  • hmm, I'm getting the same result with my own script. With or without the equates. But if it works for you, then sorry, my mistake :) – 4d4c May 03 '13 at 10:08
  • @jadkik94 did it!! :D it works just as expected and opens me an infinity of possibilities of what I was looking for, thank you!! – flipmurry May 03 '13 at 10:08