[erlang-questions] integrate rebar3 release and systemd
dieter@REDACTED
dieter@REDACTED
Tue Sep 18 13:30:38 CEST 2018
Hi all,
I spent the last two days for finding a way to integrate systemd and an erlang release,
created with rebar3.
Here are my findings, maybe they are useful for some of you as well.
My goal was to have the release to be started automatically at boot time,
and to be restarted if it totally crashes. I use Ubuntu 18.
Internal application/process restarts are handled inside erlang.
I started with a release
$ rebar3 as prod tar
and installed that into /opt/patchbox.
Then I took systemd.erl from Max Lapshin (many thanks for this!)
from his gist
https://gist.github.com/maxlapshin/01773f0fca706acdcb4acb77d91d78bb (https://gist.github.com/maxlapshin/01773f0fca706acdcb4acb77d91d78bb)
Here I added a function for writing a PID file,
and integrated this with ready() and watchdog(),
so by calling systemd:start_link(PidFile) from my top supervisor
after a successful startup, the PID file is created and READY
is sent to systemd. (And the WATCHDOG is started).
write_pid(PidFile) ->
{ok, F} = file:open(PidFile, [write, raw]),
ok = file:write(F, os:getpid()),
ok = file:close(F).
init(PidFile) ->
write_pid(PidFile),
erlang:send_after( 100, self(), ready),
erlang:send_after(60000, self(), watchdog),
{ok, state}.
handle_info(ready, State) ->
ready(),
{noreply, State};
Finally, the configuration file for systemd is this one,
I saved it in /etc/systemd/system/patchbox.service
[Unit]
Description=Patchbox
After=network-online.target
Wants=network-online.target
[Service]
# The start script forks the erlang VM, the means that the systemd's PID
# will soon be gone, and the READY message will come from an orphan
Type=forking
User=patchbox
Group=patchbox
# Allow root permission for Pre commands (mkdir, chown, etc..)
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /run/patchbox
ExecStartPre=/bin/chown patchbox:patchbox /run/patchbox
# Call rebar3's start script
ExecStart=/opt/patchbox/bin/patchbox start
Restart=on-failure
TimeoutStartSec=300s
# Expect WATCHDOG at least every 2 minutes
WatchdogSec=120s
# Allow READY and WATCHDOG from other PIDs than the original
NotifyAccess=all
# systemd expects the services' PID here
PIDFile=/run/patchbox/patchbox.pid
[Install]
WantedBy=multi-user.target
Kind regards,
Dieter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20180918/8efc225c/attachment.htm>
More information about the erlang-questions
mailing list