thingweb / thingweb-directory

W3C WoT Thing Directory implementation
Other
19 stars 11 forks source link

Add instructions to start as a service #10

Open mmccool opened 6 years ago

mmccool commented 6 years ago

I want this to start as a service whenever the gateway reboots. To get this to work some specific settings are needed. It would be useful to include a systemd service file in the distribution and some instructions on how to install it. I'm using the following but it may or may not have all the things needed (for example, cleanup of the db on shutdown, restart on crash, etc):

[Unit]
Description=Thing Directory
Requires=network.target
After=network.target

[Service]
#Restart=always
#RestartSec=20
User=thingdirectory
ExecStart=/usr/bin/java -jar /opt/thingweb-directory/build/libs/thingweb-directory-0.7.jar -c 15683 -h 8080 -d /opt/thingweb-directory/db -l /opt/thingweb-directory/Lucene /opt/thingweb-directory

[Install]
WantedBy=multi-user.target

Note I run this with an account with normal user privileges created for the purpose. To install, copy the above to /etc/systemd/system/thing-directory.service and invoke

sudo systemctl enable thing-directory.service
sudo systemctl start thing-directory.service

Also, the following commands are useful for monitoring the service:

sudo systemctl status thing-directory
sudo journalctl -u thing-directory.service

And for shutting it down/turning off automatic startup on boot:

sudo systemctl stop thing-directory.service
sudo systemctl disable thing-directory.service

~

mmccool commented 6 years ago

Note also the various options (-c, -h, -d, -l) to start the service. These should also be documented somewhere. The first two are useful to remap ports. It turns out the last two MUST be specified for the service to work as they by default try to create files in / (which won't work unless you run the service as root, which is unnecessary and unsafe).

mmccool commented 6 years ago

I also install the thingweb-directory github clone under /opt/thingweb-directory. Another option (maybe better) would be to put it under /home/thingdirectory, the home directory of the user that runs the service. Yet another option would be to put the whole thing in a container (either a snap or a docker container).

mmccool commented 6 years ago

For the updated version (binary release) I am now using the following:

[Unit]
Description=Thing Directory Service
Requires=network.target
After=network.target

[Service]
Restart=always
RestartSec=20
User=thingdirectory
ExecStart=/home/thingdirectory/thingweb-directory-0.8.1/bin/thingweb-directory -h 8070 

[Install]
WantedBy=multi-user.target

Note the use of a special account (with minimum privileges) to run. I also give an example of mapping it to a different port. The documentation can show this but perhaps use the default port. My previous comments about documenting the options somewhere is still relevant (yes, they are available with -help, but that is annoying).