[Python] How to kill zombie process with ppid = 1?

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    root@9948254b178c:/usr/src/app# ps -ef
    UID PID PPID C STIME TTY TIME CMD
    root 1 0 0 01:42 ? 00:00:00 node app.js
    root 35 1 0 01:43 ? 00:00:02 [javac] <defunct>
    root 51 1 0 01:43 ? 00:00:00 [g++] <defunct>
    root 66 0 0 01:43 pts/0 00:00:00 bash
    root 73 66 0 01:43 pts/0 00:00:00 top
    root 77 66 0 01:45 pts/0 00:00:00 top
    root 79 66 0 01:51 pts/0 00:00:00 top
    root 83 66 0 01:54 pts/0 00:00:00 ps -ef


    In the table we can see that there are two zombie processes with PID 35 and 51. How could I remove it without stopping the parent (PID=1).

    Because the parent is the whole system.

    Here is a python script I am running for compiling and running different languages. (C/C++, Python3, Java)

    Note: The script for python does not create any zombie process. But the other does. I am running nodejs ecec to run this python script.

    import sys
    import subprocess
    import re

    # "python3 run.py "+ json_msg.filename +" "+extensions[json_msg.lang]+" "+json_msg.timeout
    filename = str(sys.argv[1])
    extension = str(sys.argv[2])
    timeout = str(sys.argv[3])


    # for java
    def changing_class_name():

    file = open(f"temp/{filename}.java" , 'r')

    newfile = filename
    new_file_content = str()

    for line in file:
    if re.search('^class ',line):
    line = line.strip()
    ls = line.split(" ")

    # print(ls)
    string = ls[1]
    if(string[-1] == '{'):
    new_line = line.replace(string , newfile+'{')
    new_file_content += new_line + "\n"

    elif(string[-1]!='{'):

    new_line = line.replace(string , newfile)
    new_file_content += new_line + "\n"

    else:
    new_file_content += line


    writing_file = open(f"temp/{filename}.java", "w")
    writing_file.write(new_file_content)
    writing_file.close()



    status = True

    # we have to get the input file first
    try:
    inputfile = subprocess.run(f"cd temp/ && cat input.txt" ,shell=True , stdout=subprocess.PIPE,stderr=subprocess.STDOUT , timeout=int("5"))

    except :
    result = 'Something went wrong while reading input file'
    status = False
    # print('Something went wrong while reading input file')


    # for compiling the file
    if(status):
    try:
    if(extension == "cpp" or extension == "c"):
    comp = subprocess.run(f"cd temp/ && g++ {filename}.{extension} -o {filename}" ,shell=True , stdout=subprocess.PIPE,stderr=subprocess.STDOUT , timeout=int("5"))
    if(comp.stdout.decode()):
    result = comp.stdout.decode()
    status = False

    if(extension == "java"):
    changing_class_name()
    comp = subprocess.run(f"cd temp/ && javac {filename}.java" ,shell=True , stdout=subprocess.PIPE,stderr=subprocess.STDOUT ,timeout=5 )

    if(comp.stdout.decode()):
    result = comp.stdout.decode()
    status = False

    except:
    result = 'Something went wrong while compiling the file'
    status = False


    # running the file
    if(status):
    try:
    if(extension == "py"):
    output = subprocess.run(f"cd temp/ && python3 {filename}.{extension}" ,shell=True , stdout=subprocess.PIPE, stderr=subprocess.PIPE , input=(inputfile.stdout.decode()).encode() , timeout=int(timeout))
    result = output.stdout.decode()

    elif(extension=="cpp" or extension == "c"):
    output = subprocess.run(f"cd temp/ && ./{filename}" ,shell=True , stdout=subprocess.PIPE,stderr=subprocess.PIPE , input=(inputfile.stdout.decode()).encode() , timeout=int(timeout))
    result = output.stdout.decode()

    elif(extension == "java"):
    output = subprocess.run(f"cd temp/ && java {filename}" ,shell=True , stdout=subprocess.PIPE,stderr=subprocess.PIPE , input=(inputfile.stdout.decode()).encode() , timeout=int(timeout))
    result = output.stdout.decode()

    # if there is any error we will also add the error
    if(output.stderr.decode() != ""):
    result += output.stderr.decode()
    status = False


    except Exception as e:
    result = "Time limit exceeded"
    status = False
    # print(e)



    # getting the result and writting it on output.txt
    file = open("./temp/output.txt" , "w")
    file.write(result)
    file.close()


    if(status == True):
    print('Successful' ,end="")
    else:
    print("Failed",end="")

    Continue reading...

Compartilhe esta Página