Creating a systemd Service for Monitoring

Hi!
Having a bit of a problem with the systemd service I created. I’m very new to elixir, so I might be missing something very simple.

The issue is, if I stop the service using systemd, then I can start it just fine. but if I kill the beam process, or stop the app using edeliver, then service does not restart the process.
mix edeliver stop production -> systemd does not restart the app
lsof -i:3999 && kill -9 pid -> systemd does not restart the app

Here’s my setup:
Elixir 1.6.1 with Phoenix.
Distillery and edeliver for deploy.

Here’s my .service file:

[Unit]
Description=Runner for myapp.com
After=network.target

[Service]
Type=simple
User=deploy
RemainAfterExit=yes
EnvironmentFile=/home/deploy/myapp/env.env
WorkingDirectory=/home/deploy/apps/myapp/app_release/myapp
ExecStart=/home/deploy/apps/myapp/app_release/myapp/bin/myapp start
ExecStop=/home/deploy/apps/myapp/app_release/myapp/bin/myapp stop
Restart=always

[Install]
WantedBy=multi-user.target

Any help would be very appreciated, thank you.

Edit: I think what I might need is a deploy hook in edeliver to start this systemd, so it can monitor and manage, but not sure how to do that if possible.

ok…
tried below hook, but for some reason it’s never called, so instead created a bash script to restart after each deploy:
post_start_deployed_release() {
if [ “$TARGET_MIX_ENV” = “prod” ]; then
for host in ${arr[@]}; do
ssh deploy@${host} “sudo systemctl restart myapp.service”;
done
fi
}
(taken from: Phoenix-app monitoring and systemd)

#mix edeliver build release
#mix edeliver deploy release to production

for host in cat .deliver/config | grep PRODUCTION_HOSTS | cut -d '=' -f 2 | sed -e 's/"//g'
do
echo “Restarting the remote [$host]…”
echo ssh deploy@$host sudo systemctl restart myapp.service
done

Use foreground instead of start so Systemd can hold on to the process.

@Nicd thanks for the reply, but didn’t solve the issue unfortunately. Still systemd is not aware of the server.

There is a wiki-thread available here, perhaps it can help you to solve your problem?

Have you tried without RemainAfterExit?

RemainAfterExit=
Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no.

This is used to signal services which their status should be controlled by systemd commands, and not by their “real” status, meaning that as long as a process has been successfully started, systemd will consider it active even if it exits, and will only consider it to have been “stopped” if you use systemd to stop it. If the service fails to start then systemd won’t consider it active.

(do not forget to activate the service itself and possibly reboot)

@NobbZ yeah that guide helped me a lot, but it doesn’t have much info about edeliver + systemd interaction.

@amnu3387 thanks, I’m using that option with yes. just tried it with no, and when i kill the beam process,
I’m still getting run_erl[2555]: Erlang closed the connection. in the systemctl status, but it’s not starting up automatically.