dumb-init icon indicating copy to clipboard operation
dumb-init copied to clipboard

Trap the sigterm during the stop of a container

Open faridda opened this issue 7 years ago • 11 comments

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,

faridda avatar Jun 01 '17 22:06 faridda

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.

chriskuehl avatar Jun 01 '17 22:06 chriskuehl

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.

faridda avatar Jun 02 '17 16:06 faridda

Note that -9 is SIGKILL (uncatchable), whereas SIGTERM is -15 (usually) -- you can verify on your platform with kill -l

asottile avatar Jun 02 '17 16:06 asottile

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.

faridda avatar Jun 02 '17 16:06 faridda

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 avatar Jun 02 '17 17:06 asottile

@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.

chriskuehl avatar Jun 02 '17 19:06 chriskuehl

Note that made a docker_exec bash script to handle sigterm in containers https://github.com/ceph/ceph-container/commit/ef455a92838ee0e450d145fd597002632aceed54

ErwanAliasr1 avatar Jan 19 '18 15:01 ErwanAliasr1

@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 avatar Feb 09 '21 17:02 devTechi

@devTechi thanks for the feedback, glad it helped some

ErwanAliasr1 avatar Feb 09 '21 17:02 ErwanAliasr1

@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

ErwanAliasr1 avatar Feb 09 '21 17:02 ErwanAliasr1

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 😅 )

devTechi avatar Feb 09 '21 17:02 devTechi