philosodad
philosodad

Reputation: 1808

Elixir: Stop an external system process when Application shuts down

We have an elixir application that depends on a Tomcat server. Our current strategy is to launch the .war file with a System.cmd call when the application starts up. This works, but we'd also like to shut the server down if the application (or more specifically, the GenServer that runs the startup command) exits. How can I catch the exit of the GenServer and run another System.cmd call before exiting?

Upvotes: 1

Views: 283

Answers (1)

Máté
Máté

Reputation: 2345

What you need to do there is implement a terminate/2 callback.

Here you can even handle the various types of termination reasons like :normal, :shutdown or other self-defined reasons. All you need is the following:

module MyServer do
  use GenServer
  # ...
  def terminate(reason, state) do
    # System call goes here
    System.cmd "echo", ["I'll be back"]
  end
end

Upvotes: 2

Related Questions