0

I have the same question as here with python 3.x. I tried the solution provided but it does not work for me.

Python does not wait bash script to finish and prints "end" before bash script to be terminated.

I am on windows and have cygwin isntalled.

callBash.py:

import subprocess
print("start")
subprocess.call("sleep.sh",shell=True)
print("end")

sleep.sh:

#!/bin/bash
sleep 10
Behrang
  • 13
  • 5
  • since your script is doing... nothing, are you sure you're even _running_ the script? try `check_call` instead. – Jean-François Fabre Apr 13 '18 at 14:35
  • It is running. I see the console that waits 10 seconds and then it closes. – Behrang Apr 13 '18 at 14:37
  • the problem is that it opens a new console and detaches from your current process. Try without `shell=True` – Jean-François Fabre Apr 13 '18 at 14:37
  • without shell=True i get this error massage: OSError: [WinError 193] %1 is not a valid Win32 application – Behrang Apr 13 '18 at 14:38
  • 3
    You’re on Windows? How are you running bash scripts in the first place? If it’s through Cygwin, this won’t work without the Cygwin version of Python. If it’s through MSYS, you probably want to explicitly call the MSYS `bash` with `sleep.sh` as its argument (and without `shell=True`). – abarnert Apr 13 '18 at 14:44
  • 1
    oh you're using windows... which program .sh files are associated to? Try "cmd /c sleep.sh" in a console, see if that blocks... – Jean-François Fabre Apr 13 '18 at 14:45
  • 1
    You should have specified "windows" in your question. Reading [the docs](https://docs.python.org/3.5/library/subprocess.html#windows-popen-helpers), you'll see a section [Windows Popen Helpers](https://docs.python.org/3.5/library/subprocess.html#windows-popen-helpers) which talks about creating a "startupinfo" object to pass to the Popen constructor. Looks like your code is going to get more complicated – glenn jackman Apr 13 '18 at 14:46
  • XY problem; why running bash scripts on Windows from Python when you can do everything in python? bash scripts _suck_ in Windows environment. Don't do this. – Jean-François Fabre Apr 13 '18 at 14:46
  • I am in windows and have cygwin installed. – Behrang Apr 13 '18 at 14:51
  • You need specify more for you environment, default program to run .sh file, if run in cygwin or just with cygwin installed but cygwin path was added to path, etc. With `bash sleep.sh` replacement and run in cygwin, on my pc's window7 environment, it works. – atline Apr 13 '18 at 15:01
  • So I have this very long bash script that I run in Cygwin. I decided to make a GUI with Python (tkinter). Then I get this problem that python does not wait for my script to finish and this cause some problem. – Behrang Apr 13 '18 at 15:04

1 Answers1

-1

You never check the return value of subprocess.call, so it's likely that your script doesn't even start properly, maybe because of invalid PATH / CWD or permissions or something else.

Use subprocess.check_call instead, so that an exception is raised if your script fails to run. Exceptions are not so easy to miss.

Also if you have Python 3, subprocess.run(..., check=True) is newer and generally easier to work with.

Kos
  • 70,399
  • 25
  • 169
  • 233
  • 2
    same idea here. But OP says it's running, detached/background from current process. not my DV BTW. But not the answer either. – Jean-François Fabre Apr 13 '18 at 14:45
  • Oh, I misunderstood that part. In that case I'll leave the answer here as a semi-related suggestion. – Kos Apr 13 '18 at 14:48