[molecule.utils] add kill_chroot_pids function, requires lsof (new dependency)
This commit is contained in:
@@ -22,6 +22,7 @@ import sys
|
||||
import time
|
||||
import tempfile
|
||||
import subprocess
|
||||
import signal
|
||||
import shutil
|
||||
import random
|
||||
random.seed()
|
||||
@@ -111,6 +112,28 @@ def exec_chroot_cmd(args, chroot, pre_chroot = None, env = None):
|
||||
RUNNING_PIDS.discard(pid)
|
||||
return rc
|
||||
|
||||
def kill_chroot_pids(chroot, sig = signal.SIGTERM):
|
||||
"""
|
||||
Kill stale processes inside chroot or directory
|
||||
"""
|
||||
args = ["/usr/bin/lsof", "-t", chroot]
|
||||
sts, txt = exec_cmd_get_status_output(args)
|
||||
if sts == 0:
|
||||
killed_pids = []
|
||||
for pid_str in txt.strip().split():
|
||||
try:
|
||||
pid = int(pid_str)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
try:
|
||||
os.kill(pid, sig)
|
||||
killed_pids.append(pid)
|
||||
except (OSError, IOError):
|
||||
sts = 1
|
||||
return sts
|
||||
else:
|
||||
return sts
|
||||
|
||||
def empty_dir(dest_dir):
|
||||
"""
|
||||
Remove the content of a directory in a safe way.
|
||||
|
||||
Reference in New Issue
Block a user