'Launch a Python script through screen in another python script

I have an Python Flask app, which basically tells me the current status of other apps. I'd like for this app to be able to start my other python Flask apps, in screen session. This is because I often ssh to the machine running those apps. Anyways, since all those apps have a "start.sh" script, I though I'd do it using {os.system("cd DIR; ./start.sh"). No luck, I have a OSError: [Errno 9] Bad file descriptor. Same thing using subprocess.call("./start.sh", shell=True, cwd=DIR), or os.chdir(DIR); os.system("./start.sh") I also tried all that by replacing start.sh by its content, but no luck, always the same issue. Those apps start fine when I ./start.sh in a terminal tho...



Solution 1:[1]

I create a .sh file and the path is "/home/ts/Desktop/test.sh"

#!bin/bash
echo hello

Python code is

import subprocess
subprocess.Popen(["sh", "/home/ts/Desktop/test.sh"])

Output

>>> hello

Execute by absolute path may work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CN-LanBao