Systemd script does ExecStop right after ExecStart

S4rg0n picture S4rg0n · Jun 4, 2015 · Viewed 17k times · Source

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.

Answer

Christophe picture Christophe · Oct 4, 2017

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 wail 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.