Systemd can't shutdown my foreground app cleanly

systemd managing foregrounded apps expect them to behave in a certain way, it’s mostly a thing to manage broken apps and should not be used for anything that follows proper Unix style daemon’s, which Beam backgrounded mode does.

But this is the issue, systemd expects foregrounded apps to die when their pipe closes or during certain signals, the Beam is not built for that, it is built to be a normal standard unix-style daemon. You are fighting it as you see that you currently are if you want to try it this way.

However, you could write a wrapper script that handles communication between systemd and the beam and handles the signals, just when you get a shutdown indicator then just be sure to safely and properly shut down the beam before returning, and make sure it doesn’t get brutal killed before it’s done.

It is better to follow standard daemon procedures when dealing with systemd, regardless of the type of server. ^.^;