如何使用 ppid = 1 杀死僵尸进程?

问题描述

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

在表中我们可以看到有两个僵尸进程的 PID 为 35 和 51。如何在不停止父进程的情况下删除它(PID=1)。

因为父级是整个系统。

这是我正在运行的用于编译和运行不同语言的 python 脚本。 (C/C++、python3、Java)

注意:python 脚本不会创建任何僵尸进程。但另一个可以。我正在运行 nodejs ecec 来运行这个 python 脚本。

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}",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",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}",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}",timeout=int(timeout))
            result = output.stdout.decode()

        elif(extension == "java"):
            output = subprocess.run(f"cd temp/ && java {filename}",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="")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...