Skip to content

run as Daemon

NoteFox edited this page Jun 3, 2020 · 13 revisions

The Service is made with the Intention in mind, that it runs a long time. And the executing the jar File as a User means, that the Session, in which it has been started, needs to stay open. So If we don't want that, there are other ways to archive running it as long as the Server is actively running.

The Daemon / Service

A Daemon/Service is a program that runs automatically when the computer is started and waits in the background to do its job. A service usually has no graphical user interface. To set up our "Daemon" we're gonna use our "Init-System". In most Debian-based Systems, it's called "systemd".

Set Up

  1. make sure you got are able to use sudo!
  2. `cd /etc/systemd/system``
  3. touch ohdm_download.service ("ohdm_download" is just the name, it is changeable)
  4. use your favourite file editor to edit the just created File (use sudo for that, since we are in a system directory)
  5. Add the lines:
[Unit]                                                                 # Unit header
Description = OHDM Download Service for Android App                    # Description
after=network.target                                                   # to make sure that the Service will always be started after network connections are available

[Service]                                                              # Service Header
Type=forking                                                           # we need to set the type to forking, because we use Multi-Threading
ExecStart=bash /<ServicePath>/startServer.sh                            # that's the script we are starting , more on that later
Restart=always                                                         # Configures whether the service shall be restarted when the service process exits, is killed, or a timeout is reached
RestartSec=1                                                           # Configures the time to sleep before restarting a service
User=<User>                                                            # setting the user name, under which it runs (has to be a real user name!!!)
WorkingDirectory=/<ServicePath>/                                       # Directory, the Service is working in
TimeoutSec=0                                                           # Configures the time to wait for start-up and shut-down

[Install]
WantedBy=multi-user.target                                             # sets the "wanted-by key" for multi-user system, with or without graphical login
  1. sudo systemctl enable ohdm_download.service
  2. sudo sudo systemctl daemon-reload
  3. go into your Service package
  4. touch startServer.sh
  5. use your favourite file editor to edit the just created File
  6. add the Lines :
#!/bin/bash
cd /<from root ServicePath>
java -jar runner-<current version>.jar > stdOutputtxt &> errOutput.txt
Clone this wiki locally