[SOLVED] How to issue CTRL-C to a process in a container?

May 18, 2019
231
15
38
Varies
If I want to reboot the node i have to go into a couple of containers which are running processes via tmux.

a) how would I run a
Code:
pct exec VMID 'cmd here'
to issue a CTRL-C to these processes?

b) and how would I hook A into the node shutdown routine?

PS: At this time it is not possible to daemonize these processes (they require a password).
 
you can just find their PID (either by recording it on startup, or via /proc/.. , pidof, pgrep, ...) and kill them with the proper signal ("man kill"). you could also run them with systemd and systemd-ask-password to get the password for starting up, that would allow you to use "systemctl" to stop them (but that will also only send a signal, wait, send another signal ;))
 
  • Like
Reactions: Proxygen
you can just find their PID (either by recording it on startup, or via /proc/.. , pidof, pgrep, ...) and kill them with the proper signal ("man kill"). you could also run them with systemd and systemd-ask-password to get the password for starting up, that would allow you to use "systemctl" to stop them (but that will also only send a signal, wait, send another signal ;))

thanks, killing via PID would be it. i just need to find the proper code to kill gracefully like the CTRL-C

but what about hooking it up to the shutdown routine?
 
Ctrl+C sends SIGINT to the processes and you can specify this in the kill command too
Code:
 kill -sigint PID

b) and how would I hook A into the node shutdown routine?
Why would you want that?

poweroff
Shut down and power-off the system. (...)

If combined with --force, shutdown of all running services is skipped, however all processes are killed and
all file systems are unmounted or mounted read-only, immediately followed by the powering off. [man systemctl]
So without --force it should also stop services.
 
  • Like
Reactions: Proxygen
Code:
 kill -sigint PID
Why would you want that?

because the proper way to to kill these processes is a kill -sigint/CTRL-C, and it would be useful to be able to have the node shutdown process do this to the process in each container for me, instead of using pct exec kill -sigint or going into the container to issue the CTRL-C

am i missing something?
 
First of all the processes in the containers should be visible in your host and you can send signals from there. However, this should not be necessary. If you shutdown your host it shuts down your containers. Check this with
Code:
last -x
This again tries to gracefully kill your processes.
 
Do you really, really need SIGINT or is it only about a graceful kill? On shutdown systemd sends SIGTERM to the processes, which gracefully kills a process as SIGINT does (see here).

The source code has the following lines
Code:
broadcast_signal(SIGTERM, true, true, arg_timeout);
log_info("Sending SIGKILL to remaining processes...");

broadcast_signal(SIGKILL, true, false, arg_timeout);
log_info("Sending SIGTERM to remaining processes...");
and the last lines in journalctl on the host (clusterA) show this log line (you can reproduce it with this guide)
Code:
Aug 19 10:58:13 clusterA systemd-shutdown[1]: Sending SIGTERM to remaining processes...
Aug 19 10:58:13 clusterA systemd-journald[407]: Journal stopped
 
Last edited:
  • Like
Reactions: Proxygen
Do you really, really need SIGINT or is it only about a graceful kill? On shutdown systemd sends SIGTERM to the processes, which gracefully kills a process as SIGINT does (see here).

The source code has the following lines
Code:
broadcast_signal(SIGTERM, true, true, arg_timeout);
log_info("Sending SIGKILL to remaining processes...");

broadcast_signal(SIGKILL, true, false, arg_timeout);
log_info("Sending SIGTERM to remaining processes...");
and the last lines in journalctl on the host (clusterA) show this log line (you can reproduce it with this guide)
Code:
Aug 19 10:58:13 clusterA systemd-shutdown[1]: Sending SIGTERM to remaining processes...
Aug 19 10:58:13 clusterA systemd-journald[407]: Journal stopped

ok, so it sends a SIGTERM first and if it doesn't succeed sends a SIGKILL?

and I think I need a SIGTERM because they are scripts that I adapted to be run via systemd. they are normally stopped via CTRL-C (and some spawn other processes). Does my interpretation make sense?
 
because the proper way to to kill these processes is a kill -sigint/CTRL-C, and it would be useful to be able to have the node shutdown process do this to the process in each container for me, instead of using pct exec kill -sigint or going into the container to issue the CTRL-C

am i missing something?
You dont actually need to be INSIDE the container to kill a process since its exposed to the root, but the better question why in heavens are you running a process that needs sigint to stop... I can think of no use case where its "the proper way." Do yourself a favor and daemonize it. look up systemd (or whatever the container's OS service manager) as to how to make a daemon for your program.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!