Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trap the sigterm during the stop of a container #142

Open
faridda opened this issue Jun 1, 2017 · 11 comments
Open

Trap the sigterm during the stop of a container #142

faridda opened this issue Jun 1, 2017 · 11 comments

Comments

@faridda
Copy link

faridda commented Jun 1, 2017

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,

@chriskuehl
Copy link
Contributor

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.

@faridda
Copy link
Author

faridda commented Jun 2, 2017

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.

@asottile
Copy link
Contributor

asottile commented Jun 2, 2017

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

@faridda
Copy link
Author

faridda commented Jun 2, 2017

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 faridda changed the title Trap the sigkill during the stop of a container Trap the sigterm during the stop of a container Jun 2, 2017
@asottile
Copy link
Contributor

asottile commented Jun 2, 2017

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

@chriskuehl
Copy link
Contributor

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

@ErwanAliasr1
Copy link

Note that made a docker_exec bash script to handle sigterm in containers
ceph/ceph-container@ef455a9

@devTechi
Copy link

devTechi commented Feb 9, 2021

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

@ErwanAliasr1
Copy link

@devTechi thanks for the feedback, glad it helped some

@ErwanAliasr1
Copy link

@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

@devTechi
Copy link

devTechi commented Feb 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants