35

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.

2
  • 2
    This belongs on unix.stackexchange.com . Good luck. Commented Jun 4, 2015 at 12:39
  • 1
    Don't run external scripts from systemd that actually call your services. This won't work. Have systemd actually start the services. Commented Jun 9, 2015 at 0:05

3 Answers 3

80

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for sharing this GREAT explanation; it helps, cheers. If possible request you to please do add few links for systemctl understanding too.
Did the ExecStop semantics change here? freedesktop.org/software/systemd/man/…
13

if you run

[Service]
Type=simple

than you need: RemainAfterExit=yes

OR use forking:

[Service]
Type=forking

1 Comment

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!
0

just add: RemainAfterExit=yes it work like a charm

3 Comments

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.
Hi sylvek. Thank you for this answer. Apparently there is a very similar answer but a bit more complete: 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)
I did find it useful. My systemd service is trivial and does not run a process that remains up ( hence will be killed) - I however do need distinct execution of separate command at systemctl stop . So upvoting it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.