Christian
Christian

Reputation: 124

nohup has no effect when called from background service?

I have a java application which is running as background service on a ubuntu system. The application is running under root account and is started/stopped via systemctl. The application has an auto-update feature. To run the update the application starts a runupdate.sh script.

public void installUpdate() {
    final List<String> cmd = List.of("sh",script.getAbsolutePath());
    final Process process = new ProcessBuilder(cmd)
                        .directory(script.getParentFile())
                        .inheritIO()
                        .start();
}

Then runupdate.sh calls dpkg with nohup

runupdate.sh

#!/bin/sh

BASEDIR=$(dirname "$0")
chmod -R +x $BASEDIR/myapplication_3.23.99_amd64.deb
nohup dpkg -i $BASEDIR/myapplication_3.23.99_amd64.deb >update.log 2>&1 &

But it looks like the update process is interrupted when the service is stopped. This is the content of update.log. (unfortunately my system is in german)

(Lese Datenbank ... 275752 Dateien und Verzeichnisse sind derzeit installiert.)
Vorbereitung zum Entpacken von .../myapplication_3.23.99_amd64.deb ...
Removed /etc/systemd/system/multi-user.target.wants/myapplication.service.

The package remains in this state:

Status: install reinstreq half-configured

When I start runupdate.sh from the terminal directy, everything works fine.

Has anyone an explanation for this behaviour or an idea how to prevent interruption of the update process?

Upvotes: 0

Views: 122

Answers (1)

Christian
Christian

Reputation: 124

When the service is stopped, a SIGTERM signal is send to the process and its child processes by default. As @teapot418 pointed out, nohup does not help with that signal.

The solution in my case was to change the killMode in the service configuration file to process . Now the service itself is killed, but the update script keeps running.

Upvotes: 0

Related Questions