0
votes

I'm running a Python script from my NGINX server that runs this command

subprocess.call(["sh", "/runscript.sh", arg1, arg2, arg3], shell=False)

Problem is that when my server kill the script execution, the subprocess runned can't stop, just run forever.

That's a huge problem.

Already tried to change shell=True/shell=False.

EDIT I've implemented the code inside the sh script inside the python script. So now process start directly from subprocess.call.

There is a ways to save the PID of processes started from subprocess.call and end when task does not have input?

1
I'm not versed enough in the finer points of python multi-tasking to say if this question is a duplicate, but it looks like you may be able to derive a usable answer from the answers over here. - ShapeOfMatter

1 Answers

0
votes

First, you can:

import os

os.system("tasklist > task.temp")
with open("task.temp", "r") as f:
    print(f.read())
task = input("Enter process: ")
os.system("taskkill /f /im "+task)

After you find out which task name belongs to runscript.sh, you can replace it to:

import os

os.system("taskkill /f /im "+task) 

Where task is the name of the process you want to terminate.