How to stop already started (in background process) server binary file from Elixir code?

Hey, I’m trying to run binary with server in background and I don’t know how to stop it.
My code:

args = []
name = "example_server"
# check if example_server is in PATH environment variable
if System.find_executable(name) do
  pid = spawn(fn -> System.cmd(name, args) end) # initialize: start server
  # real work here ...
  Process.exit(pid, :normal) # clean-up: here I'm trying to stop server
  throw "oops :-)"

I through that &System.cmd/2 will be stopped if parent (spawned) process exits, but it’s not working.
Can you help me with it?

if nothing else works - you can look at the different kill cmds and call it with System.cmd:

eg “pkill -9 -P #{pid}” or whatever “kill” is appropriate for what you are doing.

for that specific pkill I think you still have to clean up with a Process.exit(pid, :normal) afterwards to take the ‘parent’ out as well.

1 Like

You also might want to consider using something like erlexec