Why is systemd stopping service immediately after it is started?

RajSanpui picture RajSanpui · Jan 5, 2016 · Viewed 17.5k times · Source

I created a systemd service which should invoke a shell script, when started or on reboot.

[Unit]
Description=Starts the DCCA index software

[Install]
WantedBy=multi-user.target

[Service]
ExecStart=/opt/insiteone/bin/indexControl start
ExecStop=/opt/insiteone/bin/indexControl stop

# Execute pre and post scripts as root
#PermissionsStartOnly=true
Restart=on-abort
TimeoutSec=600

Initially it kept on restarting in infinite loop as soon as it is started, but when i added the TimeoutSec option, it called the ExecStop as soon as the service was started for the first time (started, and then stopped again immediately).

Any clue, where i am going wrong? P.S: indexControl is a shell script, which starts other processes.

Answer

Jay Baker picture Jay Baker · Jan 5, 2016

Try changing Restart=on-abort to Restart=on-abnormal

From http://www.freedesktop.org/software/systemd/man/systemd.service.html:

Setting this to on-failure is the recommended choice for long-running services, in order to increase reliability by attempting automatic recovery from errors. For services that shall be able to terminate on their own choice (and avoid immediate restarting), on-abnormal is an alternative choice.

Also, you may want to add Type=oneshot to the [Service] section.

From https://wiki.archlinux.org/index.php/Systemd#Service_types:

Type=oneshot: this is useful for scripts that do a single job and then exit. You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited.

You can paste my recommended changes below:

[Unit]
Description=Starts the DCCA index software

[Install]
WantedBy=multi-user.target

[Service]
Type=oneshot
ExecStart=/opt/insiteone/bin/indexControl start
ExecStop=/opt/insiteone/bin/indexControl stop
Restart=on-abnormal

Something else to consider is whether or not you even need the Restart= line ... Does the script this service file calls fail often?