python subprocess.run() doesn't wait for sh script completion

5,669

subprocess.run() doesn't have this information. In fact, even tmux doesn't have this information.

You aren't telling tmux to run a command and wait; you're telling it to simulate some keypresses. It even doesn't know that those keypresses actually correspond to a script, much less know when that script is going to finish.

This is a poor method of running a script, but if you must use it, then your options are limited:

  • You can poll (periodically check) whether the file has showed up.

    while not os.path.exists(...):
        time.sleep(1)
    
  • You can use inotify (Linux) or kqueue (FreeBSD) to watch the directory; the kernel will tell you when the file shows up.

  • You can create a named pipe and try to read from it. This will wait until another program (e.g. your script) actually writes something there. (This of course requires adding an 'echo' command at the end of the script, or after the 'source' command.)

  • You can do the above using some other form of IPC (e.g. signals instead of pipes).

  • You can even use tmux itself for signalling:

    1. After running this command, run and wait for tmux wait -L myscript;

    2. Edit your script or the 'source' command to run tmux wait -U myscript when finished.

Share:
5,669

Related videos on Youtube

Peter
Author by

Peter

Scientific researcher @ IIMCB Warszawa.

Updated on September 18, 2022

Comments

  • Peter
    Peter over 1 year

    I'm trying to execute the following command from python subprocess.run():

    tmux send-keys -t sessionp:4 "source /home/user/script.sh
    

    In order to run a bash shell script into a tmux session.

    cmd = 'tmux send-keys -t sessionp:4 "source /home/user/script.sh'
    p = subprocess.run(cmd, shell=True, check=True)
    

    Python doesn't wait till the end of the script.sh execution and since the next python part of the script requires a file that is produced by script.sh, crashes.

    How can I make subprocess wait till the end of the script.sh execution?

    • Seth
      Seth about 5 years
      You're missing quotes in your CMD. Did you have a look at the CompletedProcess object?
    • Nathan.Eilisha Shiraini
      Nathan.Eilisha Shiraini about 5 years
      Are you sure tmux waits for the command to run after sending a key sequence? Maybe what you're doing is actually running the script asynchronously.