dumb-init
dumb-init copied to clipboard
Trap the sigterm during the stop of a container
Hi,
I would like to know if with dumb-init it's possible to trap the sigkill to be able to do something else during the stop of the container ?
I used keepalived and two process was created but the second one doesn't have the pid 1 for parent so it's not killed correctly.
root 7711 1 0 18:15 ? 00:00:00 keepalived -P xxx
root 7714 7711 0 18:15 ? 00:00:00 keepalived -P xxx
Regards,
Unfortunately it's not possible to catch SIGKILL at all on Linux. There's not really anything dumb-init (or any init system) can do to help here. If dumb-init gets SIGKILL, the kernel will always immediately kill it.
Hi, thanks for the reply. My bad it was not a sigkill but a sigterm. The process is kill with the -9. I just want to do a graceful kill of keepalived during the stop of the container. If i do manualy pkill keepalived and after that i stop the container, everything was fine.
Note that -9
is SIGKILL
(uncatchable), whereas SIGTERM
is -15
(usually) -- you can verify on your platform with kill -l
Yes, but when i stop my container, the process keepalived is not stop with SIGTERM. So i need to sigterm keepalived and after that stop my container. I want to script that part in the container.
Which command are you using to stop your docker container currently?
I'm seeing SIGTERM passed to children:
FROM ckuehl/dumb-init:ubuntu-trusty
ENTRYPOINT ["dumb-init", "-v", "--"]
$ docker run -t test sleep 100
[dumb-init] Child spawned with PID 5.
[dumb-init] setsid complete.
[dumb-init] Received signal 28.
[dumb-init] Forwarded signal 28 to children.
[dumb-init] Received signal 15.
[dumb-init] Forwarded signal 15 to children.
[dumb-init] Received signal 17.
[dumb-init] A child with PID 5 was terminated by signal 15.
[dumb-init] Forwarded signal 15 to children.
[dumb-init] Child exited with status 143. Goodbye.
$ docker stop 7d60fcf5663c
@asottile brings up a good idea too -- it'd be useful to add "--verbose" to your dumb-init command and then see what gets printed. That would help us debug what's happening here.
Note that made a docker_exec bash script to handle sigterm in containers https://github.com/ceph/ceph-container/commit/ef455a92838ee0e450d145fd597002632aceed54
@ErwanAliasr1
Many, many thanks for this code snippet. I was searching some time for a solution to properly handle sigterms for a child process of dumb-init
.
I didn't have the idea to override the exec function. Really nice. And again. Thank you. Works really great!
@devTechi thanks for the feedback, glad it helped some
@devTechi You can consider looking at the latest version of it if you like it : https://github.com/ceph/ceph-container/blob/master/src/daemon/docker_exec.sh
Thanks again. I really do like it. I don't think, that I need the updated version. I just need to handle every SIGNAL I want in a signle function, but who knows. May this will change tomorrow (or even today 😅 )