Skip to content

run as Daemon

NoteFx edited this page May 10, 2021 · 13 revisions

This Service is made with the Intention in mind, that it runs without any interruptions, but executing the jar File as a User means, that the Session, in which it has been started, needs to stay open. So since we don't want that, there are other ways to archive running jar Files, as long as the Server is active.

The Daemon / Service

A Daemon/Service is a program that runs in the background without a needed User-Session 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 your are able to use sudo!

  2. cd /etc/systemd/system

  3. sudo touch ohdm_download.service (this will be our name of our Service)

  4. use your favourite file editor to edit the just created File (don't forget that we are in a System direcory, so we need to use sudo)

  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. create a new File by using 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-jdk-path> -jar runner-<current version>.jar > stdOutputtxt &> errOutput.txt
  1. sudo chmod +x startServer.sh
  2. sudo systemctl start ohdm_download.service You know that this Command ran successfully, if nothing happens and your command line seems stuck, just exit it by pressing ctl-c and your service should run fine now.

If you want to know your service status, just type in
sudo systemctl status ohdm_download.service

Clone this wiki locally