31

Here's my Systemd script:

[Unit]
Description=RDS Services

[Service]
WorkingDirectory=/home/rdsdb2/script_rds/
Type=oneshot
ExecStart=/bin/bash start_services.sh
ExecStop=/bin/bash stop_services.sh
KillMode=process

[Install]
WantedBy=multi-user.target

I can't figure out why it executes sequentially (at system start or when i start it manually) ExecStart and ExecStop.

Can you help me?

Thanks in advance.

Alby11
  • 607
  • 1
  • 7
  • 15

3 Answers3

72

Type=oneshot is used for units, such as a filesystem check or a cleanup, which execute an action without keeping active processes. Such systemd units will wait until the process specified by ExecStart terminates, and then deactivate by running the process specified by ExecStop.

Type=simple (the default setting) is used when the process configured with ExecStart is the main process of the service. Such units will wait until the process specified by ExecStart returns, then deactivate by running the process specified by ExecStop.

With RemainAfterExit=yes, the service will be considered active even when all its processes have returned, and therefore the process specified by ExecStop will not run automatically. However, this setting is not recommended since the service will still appear as being active even if it has crashed. This setting is disabled by default.

Type=forking is used when the process specified by ExecStart is expected to exit after start-up is complete, while its child process(es) continue(s) to run in the background. This is the behavior of traditional UNIX daemons and the recommended choice in your case. The ExecStop setting is optional and is used to communicate with the service for a clean termination. The process specified by ExecStop will run in case the service crashes. In the absence of any ExecStop option, the systemctl stop servicename command will simply kill the remaining processes of the unit, as specified by the KillMode option.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Christophe
  • 821
  • 6
  • 2
  • 1
    Thanks for sharing this GREAT explanation; it helps, cheers. If possible request you to please do add few links for systemctl understanding too. – RavinderSingh13 Jun 05 '19 at 05:50
  • Did the ExecStop semantics change here? https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStop= – covener Mar 18 '21 at 13:11
13

if you run

[Service]
Type=simple

than you need: RemainAfterExit=yes

OR use forking:

[Service]
Type=forking
xsor
  • 1,074
  • 11
  • 11
  • I had "simple" and the app was constantly turning on and off, doing nothing. WHen I inserted RemainAfterExist=yes, then it actually ran. Thanks @xsor! – pauljohn32 Jun 15 '20 at 23:37
0

just add: RemainAfterExit=yes it work like a charm

sylvek
  • 89
  • 2
  • 2
    Not a good idea because if the process is killed (not stopped), it will still appear as running: RemainAfterExit= Takes a boolean value that specifies whether the service shall be considered active even when all its processes exited. Defaults to no. – Emil Burzo Feb 07 '17 at 10:47
  • Hi sylvek. Thank you for this answer. Apparently there is a very similar answer but a bit more complete: https://stackoverflow.com/a/39969975/3451846 if you don't want downvotes from other people I can suggest to delete this answer (you will also win a badge I think for this usually, the first time at least) – Valerio Bozz Jan 16 '23 at 08:56