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.
Running Commands in gnome-terminal Let's start gnome-terminal and run a sequence of bash commands inside using the '–' separator. The double hyphen '–' denotes the end of options to gnome-terminal. Everything after this mark is regarded as bash commands.
clear will clear your terminal from all previous content. exit will close your terminal and (this is not a command but it's cool too) ctrl+alt+t will open a new terminal for you. By pressing up and down keys you can navigate through the previous commands you entered.
Multiple terminal sessions may be organized within single GNOME Terminal window as tabs. Switching between active session is possible either by using keyboard shortcuts or by using tab bar – a row of buttons, each corresponding to active session, that appears on top of GNOME Terminal window when multiple tabs are used.
To open a terminal, you can press Ctrl, Alt and T keys together. It's not that complicate. Press and hold Ctrl first and then press Alt key and hold on to it as well. When you are holding both Ctrl and Alt keys, press T and you'll see that a new terminal window is opened.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With