Search code examples
pythonsubprocessubuntu-20.04

Subprocess.run timeout not killing subprocess


I'm currently on python-3.8.10 on Ubuntu 20 with the following code. And despite every attempt the timeout does not seem to work. I'm unsure if I am inputing the text wrong?

    cmd = 'ebook-convert "%s" "%s"' % (filename, outfile)
    process = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=77400)

    try:
        ConversionFile.set_to_finished(options.get('uuid'))

        if process.stderr and "error" in process.stderr.decode("utf-8"):
            return {'error': process.stderr.decode("utf-8")}
        return [outfile]
    except Exception as e:
        print(str(e))

Yet the program runs longer than the timeout and never gets killed


Solution

  • Adding the exec command to the subprocess commands worked based on this stack overflow answer How to terminate a python subprocess launched with shell=True

    It was also noted in the comments that another solution is taking out shell=True and changing up the commands to be in a list instead of a string. Although this may be correct, there are too many different functions in the actual file and I posted a simplified command of one of the almost hundred different subprocesses.

    Hence, why went with just simply adding the exec command to each subprocess.