What is the best way to terminate a Phoenix application having the PID

What is the best way to stop a Pheonix application from within a bash script? I have the PID of the process, how to stop it in a safe manner (i.e. giving it the time it needs to process fully current requests / database transactions)?

You could try integrating this : https://github.com/lyokato/the_end

Build a release using distillery

Create a custom handler script - and configure for the release

Then invoke the handler script when you want to achieve a graceful shutdown.

For bonus points you could implement a POSIX signal handler using something like tsutsu/signal_handler - or roll your own - and have it invoke your hander - this would also have the advantage you could just send a kill signal to the Elixir process (in the operating system sense of the word).

3 Likes

@bryanhuntesl Thank you, I appreciate your suggestion and I will try to implement it later. In the meantime, I am doing it like this:

kill -s TERM $(lsof -t -i :4060 -s TCP:LISTEN) where 4060 is the port used by the Phoenix application.

Ah that’s much easier - just create a release using distillery (or whatever) and execute it’s stop / start commands - things get weird between the different operating system flavors - and distillery does not just send an OS signal - it actually connects to the node and invokes a purpose built shut-down command -

2 Likes

Here’s the top level implementation of what it does - i.e. it does all that stuff - then finally invokes the OS level kill - https://github.com/bitwalker/distillery/blob/b945641ebbc17848378f4cd0f1c116ec78326ad4/priv/libexec/commands/stop.sh#L10-L20

Really building releases is the way to go

2 Likes