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

Solution 1:

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.