Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Systemd script does ExecStop right after ExecStart

Tags:

bash

systemd

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.

like image 952
S4rg0n Avatar asked Jun 04 '15 09:06

S4rg0n


People also ask

What is ExecStop systemd?

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.

What is ExecStart Service file?

ExecStart. The command to run to start the service. This includes the full path to the command and arguments to modify the service. The resulting [Service] section looks like this: [Service] Type=simple ExecStart=/usr/bin/sleep infinity.

How do you restart a systemd service?

Control whether the service starts with the system You can use the enable and disable subcommands to manage those defaults. Reboot the system with reboot sudo systemctl reboot , and the service won't automatically start.

How does systemd work?

systemd provides aggressive parallelization capabilities, uses socket and D-Bus activation for starting services, offers on-demand starting of daemons, keeps track of processes using Linux control groups, maintains mount and automount points, and implements an elaborate transactional dependency-based service control ...


Video Answer


2 Answers

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.

like image 179
Christophe Avatar answered Oct 20 '22 07:10

Christophe


if you run

[Service]
Type=simple

than you need: RemainAfterExit=yes

OR use forking:

[Service]
Type=forking
like image 23
xsor Avatar answered Oct 20 '22 06:10

xsor