Score:0

StartExecPost calling the service itself

cn flag

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

Ginnungagap avatar
gu flag
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.
Score:0
in flag

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.

iAmoric avatar
cn flag
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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.