0

I am trying to write a python script that does a few things:

  1. Open a terminal, cd's to a directory, and run's a command there.

  2. Open a second terminal, cd's to another directory, and executes a file there.

The execution of step 1 can only be completed after step 2 has been completed. Step 1 and 2 should both be done from a python (or another language) script.

I tries using subprocess.Popen, subprocess.call, os.sytem, but this does not seem to work.

Does anyone have an idea how to do this?

The code I have so far:

import subprocess

terminal1 = subprocess.Popen(["gnome-terminal",'cd ~',"torcs -r ~/quickrace.xml"])
terminal2 = subprocess.Popen(['gnome-terminal','cd ~'])
8
  • Show us the code! Commented Nov 21, 2017 at 12:01
  • 1
    What do you mean by "does not seem to work"? What happens actually? And yes, please also post the code you've tried. Commented Nov 21, 2017 at 12:03
  • 1
    Why do you need a terminal? Commented Nov 21, 2017 at 12:05
  • 2
    I'm still not sure why you would need a terminal for that. Does the user have to interact somehow with the terminal? Commented Nov 21, 2017 at 12:09
  • 1
    stackoverflow.com/questions/4633738/… Commented Nov 21, 2017 at 12:10

1 Answer 1

1

I don't know if it's exactly what you want but try it, replace the $variables by what you want and i think it will run.

#!/bin/bash


xterm -e "pwd ; cd $PATH ; your command ; echo press RETURN to close this window ; read" # delete the echo and the read to don't stop the process and make it run quickly
xterm -e "pwd ; cd $OTHERPATH ; your command ; echo press RETURN to close this window ; read"

And think to install xterm if you don't have. (rpm -i xterm if you have rpm packages)

In python i made this but it's not exactly what you want, but it's a beginning i think.

# -*- coding: utf-8 -*-

import os

if __name__ == "__main__":
    os.system('xterm -e "pwd ; cd $PATH ; your command ; echo press RETURN to close this window ; read" &') # delete the echo and the read to don't stop the process and make it run quickly
    input("Press Enter to continue...")
    os.system('xterm -e "pwd ; cd $OTHERPATH ; your command ; echo press RETURN to close this window ; read" &')
Sign up to request clarification or add additional context in comments.

3 Comments

Question was asked for python script
Thanks a lot, this works! Now my final question is whether it is possible to run this in a loop. Such that both windows automatically close when the processes are finished and when they are both closed they should start again. Is that possible?
It is but it's harder, you can place it in a while (true) with a sleep(x secondes) at the place of the input, but it's not a really good solution.The good solution could be to check the PID (processus ID) of the terms (it's a value which indicate the processus within run the xterm) and when the processus is over you start the next, so a while which contain a while which look the PID of xterm and close xterm when it's over and start a second xterm in a while. I'm at my work and i'm not a big hardcoder python so i can't check it now sorry. But here is my mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.