15

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.

RajSanpui
  • 11,556
  • 32
  • 79
  • 146
  • `chkconfig index off` is of no help as well (index.service is the ssystemd service file) – RajSanpui Jan 05 '16 at 15:36
  • 1
    I got the answer here: http://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started which fixed the issue – RajSanpui Jan 06 '16 at 12:16

1 Answers1

7

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?

Jay Baker
  • 101
  • 1
  • 6
  • 5
    Thanks for your answer but `type=oneshot` does not work, but `type=forking` does. I got the answer from here, which works. http://superuser.com/questions/1022142/why-is-systemd-stopping-service-immediately-after-it-is-started/ – RajSanpui Jan 06 '16 at 08:45
  • 3
    With RemainAfterExit=yes and Type=oneshot maybe work – AkisC Apr 13 '16 at 18:47
  • 2
    Cannot set `Restart=on-abnormal` on **oneshot** services, only `Restart=no` is allowed: `Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.` – s1moner3d Sep 07 '17 at 09:28
  • 1
    @AkisC Your suggestion for RemainAfterExit=yes and Type=oneshot worked! Thanks – Cardin Sep 12 '18 at 05:41