2

When we create and start a system service running, to run a script, if we include Restart=always in the .service file, does this mean that it again executes ExecStart after the script finishes and hence keeps executing the script in a loop?

I'm looking at a service file in a project, which is below, and trying to understand exactly what is happening here. Does app_script.sh start get called continuously, or only if the system service stops? I'm confused about this.

[Unit]
Description=application script 
ConditionPathExists=/application
After=network.target app.service data.service 

[Service]
Type=simple
ExecStart=/application/app_script.sh start
ExecStop=/application/app_script.sh stop
#RemainAfterExit=yes
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target
5
  • Does /application/app_script.sh start exit after the app has started? Also, have you seen man systemd.service? It says Restart=always will restart even if it was a clean exit.
    – muru
    Jun 12, 2019 at 12:53
  • @muru I saw it of course, however I still don't understand it. "will restart even if it was a clean exit" : If what exits? The script? the service itself? Jun 12, 2019 at 14:10
  • Whatever is run by any ExecStart, ExecStartPre and all the others specified there. Hence the question: does this app_script.sh exit after the app has started?
    – muru
    Jun 12, 2019 at 14:20
  • In the case I enable RemainAfterExit=yes , what is the situation then, the service will remain active and won't restart? Jun 12, 2019 at 14:33
  • RemainAfterExit has nothing do with this. As the docs say, this is about restarting when the process has exited. RemainAfterExit is only about whether the service is considered active or not.
    – muru
    Jun 12, 2019 at 14:36

1 Answer 1

2

If we include Restart=always in the .service file, does this mean that it again executes ExecStart after the script finishes and hence keeps executing the script in a loop?

The Restart option is only invoked if the started process exits; with always, according to the systemd.service documentation, the service will be restarted if the process exits due to:

  • Clean exit code or signal
  • Unclean exit code
  • Unclean signal
  • Timeout
  • Watchdog

The documentation goes on to say:

As exceptions to the setting above, the service will not be restarted if the exit code or signal is specified in RestartPreventExitStatus= (see below) or the service is stopped with systemctl stop or an equivalent operation. Also, the services will always be restarted if the exit code or signal is specified in RestartForceExitStatus= (see below).

Since you are not configuring a RestartForceExitStatus nor a RestartPreventExitStatus, the process will only be stopped by systemd during a systemctl stop ("or equivalent operation").

Does app_script.sh start get called continuously, or only if the system service stops?

Neither, as far as that goes. It does not get called continuously, as described above. Neither does it get called if the service stops (apart from calling /application/app_script.sh stop, of course); only when the process started by the service stops.

Do note that a Type=simple service instructs systemd to consider the ExecStart parameter to be the actual service. Your naming convention of /application/app_script.sh start implies that there's a shell script involved which probably invokes one or more other scripts or binaries. If your shell script forks off that process and itself exits, systemd will consider your service process to have exited.

2
  • Thanks for the detailed answer. I'm still a bit confused tho.After the script app_script.sh finishes executing, will the process exit? Jun 16, 2019 at 20:28
  • I'm a bit confused by your question; you say the app_script.sh finishes executing, so ... of course that process exits.
    – Jeff Schaller
    Jun 20, 2019 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.