A Simple Systemd Service for NATS
2016-04-09Here's a simple NATS gnatsd
service for
Systemd,
which starts gnatsd
on boot and restarts it in case it
crashes (which is highly unlikely). I assume you've already
downloaded the gnatsd
executable and placed it in /opt/bin/gnatsd
.
Save the configuraton below as /etc/systemd/system/gnatsd.service
:
[Unit]
Description=NATS server
[Service]
ExecStart=/opt/bin/gnatsd
Restart=on-failure
[Install]
WantedBy=multi-user.target
You can now start the service using:
$ sudo systemctl start gnatsd.service
Confirm that gnatsd
is running:
$ printf "PING\r\n" | nc localhost 4222
# Output:
INFO {"server_id":"68348d45b8246e8c1a1fb6c37a940d61","version":"0.7.2","go":"go1.5.2","host":"0.0.0.0","port":4222,"auth_required":false,"ssl_required":false,"tls_required":false,"tls_verify":false,"max_payload":1048576}
PONG
Retrieve info about the service:
$ systemctl status gnatsd.service
# Output:
● gnatsd.service - NATS server
Loaded: loaded (/etc/systemd/system/gnatsd.service; disabled; vendor preset: enabled)
Active: active (running) since Fri 2016-04-08 21:02:23 EDT; 2min 47s ago
Main PID: 1726 (gnatsd)
CGroup: /system.slice/gnatsd.service
└─1726 /opt/bin/gnatsd
In order to make the service start on boot, we need to enable it:
$ sudo systemctl enable gnatsd.service
You can restart the service (e.g., when the gnatsd
configuration changes):
$ sudo systemctl restart gnatsd.service
gnatsd
will support reloading the configuration without restarting soon, so the
following will be available:
$ sudo systemctl reload gnatsd.service
You can stop the service:
$ sudo systemctl stop gnatsd.service
Or disable it, so it won't start on boot:
$ sudo systemctl disable gnatsd.service