PDA

View Full Version : [Python] Open a new terminal window



Pyro.699
October 1st, 2009, 01:35 PM
Hey,

Ive been looking at the os modules, like execv and execl and system and popen, etc... but they weren't working like id want.

I'm basically building a "startup script"; which is something that when the computer is turned on this script is run, and will run other (separate) scripts from it. So heres how it would work:

###
os.open_my_process('"C:\windows\system32\cmd.exe" /K C:/Startup/MyScript1.py')
os.open_my_process('"C:\windows\system32\cmd.exe" /K C:/Startup/MyScript2.py')
os.open_my_process('"C:\windows\system32\cmd.exe" /K C:/Startup/MyScript3.py')
###

(Sorry for the windows process' i forget the path of the linux terminal xP)

After my script had run the "startup script" would close and MyScript[1-3].py would continue to run.

Thanks
~Cody Woolaver

nisshh
October 1st, 2009, 01:39 PM
Im a little confused, do you need help with the code, or do you need to know how to run one script from inside another?

Pyro.699
October 1st, 2009, 01:46 PM
I need to know how to run one script from within another, but in a seperate process. Not new thread, but an entirely new process window.

So with the example in the first post it would open 3 terminals, and in terminal 1, myscript1.py would be running, in terminal 2 myscript2.py, would be running and so on.

Thanks

nisshh
October 1st, 2009, 06:18 PM
Ok well can you post your code please.

Pyro.699
October 1st, 2009, 06:24 PM
There isnt any code yet ^^ its only going to be a 3 or 4 lined file, i dont know the proper code to use to open up a new window/process.

nisshh
October 1st, 2009, 06:57 PM
Well for what you want i can tell you right now its going to be atleast 10 lines.

EDIT: Why dont you just run multiple scripts at startup?

days_of_ruin
October 1st, 2009, 10:10 PM
import subprocess

pid = subprocess.Popen(args=[
"gnome-terminal", "--command=python test.py"]).pid
print pid

Replace test.py with whatever python script you want.
Of course just repeat this for as many different scripts you
want to run.

Just curious, why do you want these to be running in a terminal?

kumoshk
October 2nd, 2009, 07:35 AM
Hmm, it sounds to me like you want to know how to open a separate process from the terminal, and not necessarily from pure Python-code.

Here's an example of something like what I think you want in Linux:
gnome-terminal -e 'bash -c "./FileName; sleep 999999999d"'

Anyway, that'll open up a new gnome-terminal (even if you're in a gnome-terminal) and execute FileName and then it'll sleep (pause) for about a billion days. This new gnome-terminal will close by itself after all processes complete (so if you need to see the output, the sleep command might be useful).

If you're not familiar with Linux, you might want to know the following:
• ./fileName will execute fileName (provided it has correct permissions)
• A semicolon can be used to add additional commands to one line
• bash -c invokes BASH (a shell) to execute your commands

To open a new process from the command-line in Windows, just prepend the 'start' command to whatever you want to run: i.e. start python fileName.py (although I don't think this will open a terminal window for you).

With this knowledge you can just execute those terminal commands from Python (although they are OS-specific; I don't know what to tell you for Macintosh, but I imagine it's similar to Linux).

Does this help at all?