2

I have a remote server running CentOS 7 I can only access by SSH. I want two java servers running on them at all times, even after ISP does reboots etc. S

So I have tried to make a systemd service that starts the two java servers in a screen. I do not get any error messages when I start the service but it instantly dies: (systemctl status -l blogpatcher.service)

    * blogpatcher.service - Start blogpatcher servers
Loaded: loaded (/etc/systemd/system/blogpatcher.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sat 2020-02-08 04:19:09 EST; 7s ago
Process: 22388 ExecStart=/usr/bin/bash /home/blogpatc/script/blogpatcher.sh (code=exited, status=0/SUCCESS)
Main PID: 22388 (code=exited, status=0/SUCCESS)

Here is blogpatcher.service file:

# vi /etc/systemd/system/blogpatcher.service

[Unit]

Description=Start blogpatcher servers

After=network.target


[Service]

Type=simple

ExecStart=/usr/bin/bash /home/blogpatc/script/blogpatcher.sh

TimeoutStartSec=90


[Install]

WantedBy=default.target

Here is the script file that the service run:

# vi /var/tmp/test_script.sh
#!/bin/bash
screen -dmS syn bash -c 'cd /home/blogpatc/server/;java -cp bloghelper_artifact_main.jar com.aperico.bloghelper.server.ThesaurusServer;exec bash'

If I just run the script file from SSH console it works as intended and since there is no error message I am a bit stumped and wondering if anyone knows what the problem is?

3 Answers3

3

Adding: "RemainAfterExit=yes" under the [Service] section will let the screen remain open.

1

The problem probably is exec bash at the end. The bash gets no input and therefore it exits. If you do an exec of any shell, that does the same: no input left, so the shell exits.

But more fundamentally: why use screen? What is the advantage here?

What is the problem with just letting the systemd do the restarting?

[Unit]
Description=Start blogpatcher server  
After=network.target

[Service]   
Type=simple
ExecStart=/usr/bin/bash /home/blogpatc/script/blogpatcher.sh
TimeoutStartSec=90
Restart=always
RestartSec=10

[Install]
WantedBy=default.target

and

#!/bin/bash
cd /home/blogpatc/server/
java -cp bloghelper_artifact_main.jar com.aperico.bloghelper.server.ThesaurusServer

Systemd has a lot of additional parameters and possibilities You might want to read-up on them. For example, if the script is used just for setting the working directory (and not as a beginning for a more elaborate start-up script as I assumed), you might want to look at WorkingDirectory, as JdeBP suggested in the comments.

If you insist that screen must be used in the start-up script, then the service part of your unit file should probably be something like:

[Service]
Type=simple
Restart=on-failure
WorkingDirectory=/home/blogpatc/server/
RestartSec=3
ExecStart=/usr/bin/screen -L -dm java -cp bloghelper_artifact_main.jar com.aperico.bloghelper.server.ThesaurusServer

or explore Type=forking instead of Type=simple.

Ljm Dullaart
  • 4,643
0

You can prepend the exec command to screen which replaces the bash process with the screen process. This allows systemctl to recognise when your screen process terminates and use its restart-policy mechanism to restart it. This is the altered script:

# vi /var/tmp/test_script.sh
#!/bin/bash
exec screen -dmS syn bash -c 'cd /home/blogpatc/server/;java -cp bloghelper_artifact_main.jar com.aperico.bloghelper.server.ThesaurusServer;exec bash'

And this is how you specify your restart-policy:

[Service]
# ...
Restart=always
AdminBee
  • 22,803