0

I have a service defined as bellow:

Unit]
Description=My Service
After=network.target

[Service]
ExecStart=/usr/local/bin/myservice
KillMode=process
Restart=on-failure
ExecStartPost=/usr/local/bin/script.sh

[Install]
WantedBy=multi-user.target

and script.sh contains commands like:

while ! $(systemctl is-active --quiet myservice) ; do echo "waiting.."; sleep 5; done

(and other calls to the service)

The problem is that it doesn't work. I think it's a chicken-and-egg issue. I'm waiting for the service to be up in the service start itself. This obviously can't work.

So my question is:

How can I run script.sh automatically when myservice is up?

Thanks

iAmoric
  • 121
  • 1
  • 4
  • You're trying to use `ExecStartPost` with a service that doesn't play nice since it doesn't notify systemd when it has finished starting up, you're most likely stuck with trying whatever it is you need `ExecStartPost` until it works. – Ginnungagap Apr 06 '23 at 21:12

1 Answers1

0

From manual

ExecStartPost= commands are only run after the commands specified in ExecStart= have been invoked successfully, as determined by Type= (i.e. the process has been started for Type=simple or Type=idle, the last ExecStart= process exited successfully for Type=oneshot, the initial process exited successfully for Type=forking, "READY=1" is sent for Type=notify/Type=notify-reload, or the BusName= has been taken for Type=dbus).

So ExecStartPost will executed only after ExecStart= have been invoked successfully.

Your script doesn't work may be due to wrong options choose in unit file.

asktyagi
  • 2,860
  • 2
  • 8
  • 25
  • I investigated a bit more. Actually, having service in `running` state is not enough. The service needs to start a server and listen on port X. This can take a bit of time. Thus, ExecStartPost starts too quickly, before the server to be started. What I don't understand is that I tool this in account in my script.sh (wait for port to be open) but it's waiting forever – iAmoric Apr 05 '23 at 20:12