Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@djo
Last active January 27, 2020 04:35
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save djo/bfa9fa75928ce432ec51 to your computer and use it in GitHub Desktop.
Save djo/bfa9fa75928ce432ec51 to your computer and use it in GitHub Desktop.
Handling of UNIX-signals in Erlang/Elixir is not supported, this script provides start-stop management with handling TERM signal for Docker installation.
#!/usr/bin/env bash
set -x
term_handler() {
echo "Stopping the server process with PID $PID"
erl -noshell -name "term@127.0.0.1" -eval "rpc:call('app@127.0.0.1', init, stop, [])" -s init stop
echo "Stopped"
}
trap 'term_handler' TERM INT
elixir --name app@127.0.0.1 -S mix run --no-halt &
PID=$!
echo "Started the server process with PID $PID"
wait $PID
# remove the trap if the first signal received or 'mix run' stopped for some reason
trap - TERM INT
# return the exit status of the 'mix run'
wait $PID
EXIT_STATUS=$?
exit $EXIT_STATUS
@entone
Copy link

entone commented Jun 17, 2016

if you are using docker-compose make sure to use the entrypoint config variable and not command. command runs the file under /bin/sh -c and doesn't pass through signals.

@tommyjcarpenter
Copy link

Is this for Erlang or Elixir? I see both above. Do you have a trimmed down one for Erlang?

@nathanl
Copy link

nathanl commented Feb 5, 2019

Nice! FWIW, OTP 19.3 in Oct 2018 added handling of SIGTERM:

A received SIGTERM signal to beam will generate a 'stop' message to the init process and terminate the Erlang VM nicely. This is equivalent to calling init:stop/0
https://www.erlang.org/downloads/19.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment