0

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?

tidpe
  • 325
  • 3
  • 15
  • 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](https://stackoverflow.com/q/28025402/10135377). – ShapeOfMatter Jun 18 '20 at 15:18

1 Answers1

0

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.

Red
  • 26,798
  • 7
  • 36
  • 58
  • But can I know, when run subproccess.call, which id the PID of each subprocess derivated from it and then grab the "event" of closing script to kill them? – tidpe Jun 18 '20 at 15:24
  • If i run that script i got "sh: 1: tasklist: not found". PS: i am on ubuntu env – tidpe Jun 22 '20 at 08:13