Open new gnome-terminal and run command
Asked Answered
H

6

9

I'm trying to write a script that opens a new terminal then runs a separate python script from that terminal.

I've tried:

os.system("gnome-terminal 'python f.py'")

and

p = Popen("/usr/bin/gnome-terminal", stdin=PIPE)
p.communicate("python f.py")

but both methods only open a new terminal and do not run f.py. How would I go about opening the terminal AND running a separate script?

Edit: I would like to open a new terminal window because f.py is a simply server that is running serve_forever(). I'd like the original terminal window to stay "free" to run other commands.

Hexone answered 27/11, 2017 at 20:4 Comment(6)
you don't need to open terminal, just call your python with popen with the argumentsOrtrude
Possible duplicate of Calling an external command in PythonOrtrude
try p.communicate("python f.py\n")Ciborium
can't you run python f.py & then? do you need output from p.py ?Ciborium
Oh interesting, this is the first time I've seen the & flag. I suppose this accomplishes what I'm after.Hexone
ok, I have edited my answer. I think it pretty much covers every aspect.Ciborium
M
6

Like most terminals, gnome terminal needs options to execute commands:

gnome-terminal [-e, --command=STRING] [-x, --execute]

You probably need to add -x option:

x, --execute

Execute the remainder of the command line inside the terminal.

so:

os.system("gnome-terminal -x python f.py")

That would not run your process in the background unless you add & to your command line BTW.

The communicate attempt would need a newline for your input but should work too, but complex processes like terminals don't "like" being redirected. It seems like using an interactive tool backwards. And again, that would block until termination. What could work would be to use p.stdin.write("python f.py\n") to give control to the python script. But in that case it's unlikely to work.

So it seems that you don't even need python do to what you want. You just need to run

python f.py &

in a shell.

Mcculloch answered 27/11, 2017 at 20:9 Comment(0)
T
2

As of GNOME Terminal 3.24.2 Using VTE version 0.48.4 +GNUTLS -PCRE2

Option “-x” is deprecated and might be removed in a later version of gnome-terminal. Use “-- ” to terminate the options and put the command line to execute after it.

Thus the preferred syntax appears to be

gnome-terminal -- echo hello

rather than

gnome-terminal -x echo hello
Thermocouple answered 4/11, 2018 at 0:25 Comment(0)
O
1

Here is a complete example of how you would call a executable python file with subprocess.call Using argparse to properly parse the input.

the target process will print your given input.

Your python file to be called:

import argparse    
parser = argparse.ArgumentParser()

parser.add_argument("--file", help="Just A test", dest='myfile')
args = parser.parse_args()
print args.myfile

Your calling python file:

from subprocess import call


#call(["python","/users/dev/python/sandboxArgParse.py", "--file", "abcd.txt"])
call(["gnome-terminal", "-e", "python /users/dev/python/sandboxArgParse.py --file abcd.txt"])

Just for information: You probably don't need python calling another python script to run a terminal window with a process, but could do as follows:

gnome-terminal -e "python /yourfile.py -f yourTestfile.txt"
Ortrude answered 27/11, 2017 at 20:13 Comment(2)
Thanks for the detailed response. I clarified my question as the python script I'm calling is a simple server utilizing serve_forever(). Therefore, I'd like to run the script in a new terminal window so that my current terminal isn't tied up.Hexone
That's easy just use call with gnome-terminalOrtrude
S
1

The following code will open a new terminal and execute the process:

process = subprocess.Popen(
    "sudo gnome-terminal -x python f.py", 
    stdout=subprocess.PIPE,
    stderr=None,
    shell=True
)
Shroff answered 31/3, 2018 at 12:9 Comment(0)
A
0

I am running a uWS server with this.In my case Popen didn't help(Even though it run the executable, still it couldn't communicate with a client -: socket connection is broken).This is working.Also now they recommends to use "--" instead of "-e".

subprocess.call(['gnome-terminal', "--", "python3", "server_deployment.py"])

#server_deployment.py

def run():
    execution_cmd = "./my_executable arg1 arg2 dll_1 dll_2"
    os.system(execution_cmd)
run()
Airbrush answered 7/11, 2021 at 19:10 Comment(0)
W
0

Just execute:

import os
os.system(python f.py)

but remember that doesn't open gnome-terminal window.

Wilbertwilborn answered 10/6 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.