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?

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. Commented Jun 18, 2020 at 15:18

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
If i run that script i got "sh: 1: tasklist: not found". PS: i am on ubuntu env

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.