peterbourgon / runsvinit

A Docker init process for graceful shutdown of runit services.
MIT License
39 stars 9 forks source link

Shell script to reap orphaned processes #3

Open mitar opened 7 years ago

mitar commented 7 years ago

Wouldn't this work:

#!/bin/bash

sv_stop() {
    for s in $(ls -d /etc/service/*)
    do
        /sbin/sv stop $s
    done
}

trap "" SIGCHLD
trap "sv_stop; exit" SIGTERM
/sbin/runsvdir /etc/service &
wait

By setting handler for SIGCHLD to explicit ignore, POSIX.1-2001 allows a parent process to elect for the kernel to automatically reap child processes that terminate. trap "" SIGCHLD is the same as explicitly setting SIGCHLD handler to SIG_IGN. This is untested though. I am not sure if one can really trap SIGCHLD in bash.

Also see the script here. But I do think it has an issue.

discordianfish commented 7 years ago

Oh that would be great. You could check if this the kernel reaps the children with that bash entrypoint by using https://github.com/peterbourgon/runsvinit/blob/master/zombietest/build/zombie.c