20

I have these two services, one is Google start up script service and second one is redis service, I want to start redis service once the startup script service is started and done, I have these following systemd config but my redis services won't start with these config

google-startup-scripts.service
[Unit]
Description=Google Compute Engine Startup Scripts
After=network-online.target network.target rsyslog.service
After=google-instance-setup.service google-network-daemon.service
After=cloud-final.service multi-user.target
Wants=cloud-final.service
After=snapd.seeded.service
Wants=snapd.seeded.service

[Service] RemainAfterExit=yes ExecStart=/usr/bin/google_metadata_script_runner --script-type startup KillMode=process Type=oneshot StandardOutput=journal+console Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

[Install] WantedBy=multi-user.target


redis.service

[Unit] Description=Redis In-Memory Data Store After=google-startup-scripts.service

[Service] Type=notify PIDFile=/run/redis-6378.pid ExecStart=/usr/bin/redis-getdevice /etc/redis-getdevice/6378.conf ExecStop=/usr/bin/redis-cli -p 6378 shutdown Restart=always

[Install] WantedBy=multi-user.target

once the google-startup-script.service run and it do the operations and goes to state exited. and redis service doesn't start at all (I'm using After in unit) what I'm doing wrong here

Jack
  • 321

2 Answers2

20

There a number of different keywords to specify systemd unit dependencies. Each with a slightly different effect and implications with regards to failure handling and for example "starting" and "waiting for startup to complete" for example :

Wants

Configures (weak) requirement dependencies on other units. ... Units listed in this option will be started if the configuring unit is. ...

Requires

Similar to Wants=, but declares a stronger requirement dependency. ... If this unit gets activated, the units listed will be activated as well. If one of the other units fails to activate, and an ordering dependency After= on the failing unit is set, this unit will not be started. ...

After

... If unit foo.service contains the setting Before=bar.service and both units are being started, bar.service's start-up is delayed until foo.service has finished starting up. After= is the inverse of Before=, i.e. while Before= ensures that the configured unit is started before the listed unit begins starting up, After= ensures the opposite, that the listed unit is fully started up before the configured unit is started. ...

In that regard your redid.service Unit, with a After=google-startup-scripts.service dependancy, appears correct.

Your issue would seem to be that the google-startup-scripts.service is Type=oneshot. The systemd service manager will consider the unit up after the main ExecStart= process exits.

What can happen is that a script triggers tasks to start and run in the background and doesn't wait for those to complete (or to complete their start-up). The script will continue to the next steps and then successfully complete, systemd considers the unit to be up, but one or more processes are not quite there yet.

You may need to make an adjustment in whatever is running from /usr/bin/google_metadata_script_runner --script-type startup

Bob
  • 6,366
0

From https://www.freedesktop.org/software/systemd/man/latest/systemd.unit.html

Along with a unit file foo.service, the directory foo.service.wants/ may exist. All unit files symlinked from such a directory are implicitly added as dependencies of type Wants= to the unit. Similar functionality exists for Requires= type dependencies as well, the directory suffix is .requires/ in this case. This functionality is useful to hook units into the start-up of other units, without having to modify their unit files.

In your case your specific, you can remove [Install] section from unit file.

You could also use PartOf=systemgoogle-startup-scripts.service under the [Unit] section.

systemctl disable redis.service

systemctl stop redis.service

mkdir /usr/lib/systemd/system/systemgoogle-startup-scripts.service

cd /usr/lib/systemd/system/systemgoogle-startup-scripts.service

ln -s ../redis.service . (adjust to you needs).

systemctl enable redis.service

systemctl restart systemgoogle-startup-scripts.service

systemctl status redis.service

If all went as planned, from the output you will notice that it is now loaded as a static service and active.

MarcoP
  • 111