#!/usr/bin/env bash # I don't trust anything but the kernel to keep a sane state here, # so I don't (ab)use init to get this job done, # nor do I count on myself actually knowing what is or isn't mounted # (some packages can mount extra filesystems, like binfmt_misc). # So, for process slaughter, PREFIX=/mnt/chroot-0 FOUND=0 for ROOT in /proc/*/root; do LINK=$(readlink $ROOT) if [ "x$LINK" != "x" ]; then if [ "x${LINK:0:${#PREFIX}}" = "x$PREFIX" ]; then # this process is in the chroot... PID=$(basename $(dirname "$ROOT")) kill -9 "$PID" FOUND=1 fi fi done if [[ "x$FOUND" = "x1" ]]; then # repeat the above, # the script I'm cargo-culting this from just re-execs itself fi