4

I'm trying to create a service which runs several scripts using ExecStart and some of them can be failed. I need to ignore these failed scripts and return a success for the service.

Previously I used upstart and this is how I achieved it.

start on runlevel [23]
stop on shutdown

script
    set +e # Disable exit on non 0
    stop sys_process_script
    start sys_init_script
    start sys_process_script
    set -e # Enable exit on non 0
end script

But I don't know how to ignore the failed script. This is my implementation so far.

[Unit]
Description=System starter

[Service]
Type=forking
ExecStart=/bin/systemctl stop sys_process_script
ExecStart=/bin/systemctl start sys_init_script
ExecStart=/bin/systemctl start sys_process_script

Is there any method to ignore the script if failed in systemctl or in .service - onFailure?

Choxmi
  • 1,584
  • 4
  • 29
  • 47
  • Note that if you're using `systemd` to call `systemctl` inside a service, you're doing something wrong :-) Look up the manuals on [Target Unit Configuration](https://www.freedesktop.org/software/systemd/man/systemd.target.html#), which explains how to group a series of `systemd` commands together in an unified way. – Gwyneth Llewelyn Apr 06 '22 at 17:08

3 Answers3

3

Easy way to wrap inline

ExecStart=/bin/sh -c"command_which_will_fail; exit 0;"
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
3

I know this was asked quite a while ago, but systemd has a much simpler feature to ignore failed scripts.

Just prefix the path with a minus sign: -

e.g.

ExecStart=-/bin/systemctl stop sys_process_script
ExecStart=-/bin/systemctl start sys_init_script
ExecStart=-/bin/systemctl start sys_process_script

See an answer by @mark-stosberg on Unix StackExchange.

The reference is here: Table 1. Special executable prefixes

Gwyneth Llewelyn
  • 796
  • 1
  • 11
  • 27
0

Have that binary executable or script return an exit code of zero(0).

John Greene
  • 2,239
  • 3
  • 26
  • 37