2

I have a Java Application is going to be my Discord Server's bot.

How do I make this application run as a daemon on a Raspberry Pi, it uses the latest version of Raspbian and the Java application runs off a jar file, though it does require an env variable to set its Spring profile.

How can I make the application run as a Deamon, Plus I need it to have commands to stop and start, as I going to hook up a Jenkins Build server to stop the service, and then restart it with the new version it built.

1

The same way you make any program run as a daemon regardless of the language it has been written in!

Read the documentation for systemd. At a minimum, you will have to create something like /etc/systemd/system/yourprogram.service. There are way too many options that you need to set depending on exactly what your program does. In all likelihood, all the defaults will be fine.

3

Services are managed by systemd. Just start with a simple Unit file:

rpi ~$ sudo systemctl --full --force edit myjar.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Descrition=My jar service
After=multi-user.target

[Service]
Environment="ENV_VAR_FOR_JAR=something"
ExecStart=/absolute/path/to/myprogram.jar

[Install]
WantedBy=multi-user.target

To manage the service just use:

rpi ~$ sudo systemctl start|stop|restart myjar.service
rpi ~$ sudo systemctl --full edit myjar.service   # edit the Unit again
rpi ~$ systemctl status myjar.service

If you want to start the service at boot up, then do

rpi ~$ sudo systemctl enable myjar.service

If this doesn't run on the first attempt (very likely) then ask again so we can look what other special settings your program needs. Have a look at man systemd.exec for some options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.