mirror of
https://gitee.com/sui-feng-cb/AzurLaneAutoScript1
synced 2026-03-12 22:37:00 +08:00
48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
|
|
import os
|
|||
|
|
import time
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_process_exist(pid):
|
|||
|
|
try:
|
|||
|
|
os.kill(pid, 0)
|
|||
|
|
except OSError:
|
|||
|
|
return False
|
|||
|
|
else:
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
|
|||
|
|
def orphanSlayer(ppid, spid, prekill = ""):
|
|||
|
|
"""
|
|||
|
|
module.webui.process_manager.ProcessManager.stop() uses kill() to stop subprocess
|
|||
|
|
and to a large extent it cannot be changed to terminate(), see #883
|
|||
|
|
|
|||
|
|
In *nix systems, process created by subprosecc.Popen does not exit with the parent process,
|
|||
|
|
and there are no options of daemon=True, etc.
|
|||
|
|
So there need to be some way to ensure sub-subprocess to be killed other than capturing SIGTERM,
|
|||
|
|
such as:
|
|||
|
|
|
|||
|
|
1. Set subprocess to a new process group and killpg() in ProcessManager.stop()
|
|||
|
|
2. Try to terminate() or send other signals before kill()
|
|||
|
|
3. Make sub-subprocess end itself when it does not receive a heartbeat
|
|||
|
|
|
|||
|
|
All of these seemingly elegant solutions require invasive changes,
|
|||
|
|
therefore, I choose to open another process, once the father dead, kill the son as well
|
|||
|
|
So called orphanSlayer
|
|||
|
|
|
|||
|
|
Lme曰:「你可以通过经常拉屎,来结交朋友(」
|
|||
|
|
"""
|
|||
|
|
while is_process_exist(ppid):
|
|||
|
|
if not is_process_exist(spid):
|
|||
|
|
return
|
|||
|
|
time.sleep(1)
|
|||
|
|
if prekill:
|
|||
|
|
os.system(prekill)
|
|||
|
|
os.kill(spid, 9)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
# python orphanSlayer.py 114 514 "docker stop ..."
|
|||
|
|
import sys
|
|||
|
|
|
|||
|
|
orphanSlayer(int(sys.argv[1]), int(sys.argv[2]), sys.argv[3])
|