I am trying to continuously run a python script with random parameters from another python script, where each run is in its own tmux session. A very simplified overview of what I'm trying to do goes like this:

# Python script to run other python scripts
from subprocess import call
import random

while True
    param = randint(1,100)
    runmyscript ="tmux send-keys -t mysession"+str(param)+" 'python myscript.py param' "
    #Wait until myscript.py is done running in its tmux session <-- How to do that?

For example, let's say that the random numbers are 57, 61, 88 ... etc. The above script should run:

  • 'myscript.py 57' in a tmux session called "mysession57"
  • 'myscript.py 61' in a tmux session called "mysession61"
  • 'myscript.py 88' in a tmux session called "mysession88" ... etc

But how can I make sure that the script waits until each script in its tmux session is finished?

  • Not an answer, and I don't know tmux but... there must be a better way, a more programmatic way, of doing what you need to do than simulating keypresses in a command window to run a script (because that's what I assume tmux send-keys does). Anything that remotely resembles directly invoking some kind of command instead of simulating keypresses would have a better chance of providing you with a method of notifying the caller when it's done. – Celada Feb 12 '15 at 2:49
  • You're 100% right. But I have reached the point that I must run what I have and get my results asap. That said, I'm sure that the question could be rephrased so that it refers to the same concept but less sloppy.. – geo909 Feb 12 '15 at 2:52
from subprocess import call
import random

while True:
    param = random.randint(1,100) #add random first or from random import randint
    runmyscript ="tmux send-keys -t mysession %s 'python myscript.py param' "%str(parma)
    call(runmyscript,shell=True)#you should add if or something to break loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.