systemd-notify may not activate services properly

456 views
Skip to first unread message

Tim Schindler

unread,
Apr 9, 2014, 9:06:37 AM4/9/14
to coreo...@googlegroups.com
Hello,

I am on coreos-vagrant and try to figure out how to use systemd notifications to activate services. To do so, I run a simple service file, that calls a bash script. The bash script just calls `systemd-notify`, but a single call is not enough. To ensure the service activation I need to call `systemd-notify` multiple times. Am I doing something wrong or is that expected behaviour? Imagine having the following files, `systemd-notify` needs to be called 3 times.

Thanks guys

PS: coreos rocks! <3

foo.service
``ìni
[Service]
Type=notify
ExecStart=/bin/bash /home/core/foo.sh
NotifyAccess=all

[Unit]
Description=Foo Service
```

foo.sh
```bash
#!/bin/bash

echo "started foo"

# Notify systemd.
while [ true ]; do
  sleep 1
  systemd-notify --ready --status="foo is ready"
  echo "notified systemd"
  fleetctl list-units
done
```

output.txt
```txt
Apr 09 12:52:08 core-01 systemd[1]: Starting Foo Service...
Apr 09 12:52:08 core-01 bash[32151]: started foo
Apr 09 12:52:09 core-01 bash[32151]: notified systemd
Apr 09 12:52:09 core-01 bash[32151]: UNIT                LOAD        ACTIVE                SUB        DESC                MACHINE
Apr 09 12:52:09 core-01 bash[32151]: foo.service        loaded        activating        start        Foo Service        4cf1102d.../172.17.42.1
Apr 09 12:52:10 core-01 systemd[1]: Started Foo Service.
Apr 09 12:52:10 core-01 bash[32151]: notified systemd
Apr 09 12:52:10 core-01 bash[32151]: UNIT                LOAD        ACTIVE                SUB        DESC                MACHINE
Apr 09 12:52:10 core-01 bash[32151]: foo.service        loaded        activating        start        Foo Service        4cf1102d.../172.17.42.1
Apr 09 12:52:11 core-01 bash[32151]: notified systemd
Apr 09 12:52:11 core-01 bash[32151]: UNIT                LOAD        ACTIVE        SUB        DESC                MACHINE
Apr 09 12:52:11 core-01 bash[32151]: foo.service        loaded        active        running        Foo Service        4cf1102d.../172.17.42.1
```

Tim Schindler

unread,
Apr 9, 2014, 10:47:59 AM4/9/14
to coreo...@googlegroups.com
UPDATE:

I found a workaround for just getting my case to work. I just do the following.

```bash
# Notify systemd.
echo "Waiting for systemd..."
while [ "$(systemctl | grep foo.service | grep active)" == "" ]; do
  systemd-notify --ready --status="foo is ready"
  sleep 1
done

echo "notified systemd"
```

HIroaki Ban

unread,
Mar 27, 2016, 10:34:48 AM3/27/16
to CoreOS Dev
Hi all,

I worked on CentOs 7 and came across the same issue. 'systemd-notify' is not reliable. Sometimes it works, but sometimes it doesn't. I found this topic and tried the way which is written in this topic, but it didn't work well in my case.
But finally I've found a solution.

Solution in bash
--------------------------------------------------------------------------------------
yum install  --enablerepo=epel socat
then use
echo -en 'READY=1' | socat STDIO ${NOTIFY_SOCKET}
in stead of
systemd-notify --ready
--------------------------------------------------------------------------------------

The trick is that, 'systemd-notify' is based on UNIX domain socket, and the name of the socket is informed by environment variable $NOTIFY_SOCKET. So notifying directly to the socket solves this problem. This works fine for me.

Hope this helps.

Appendix
In Python, you can do the same thing in two ways.

Solution 1 in Python

--------------------------------------------------------------------------------------
yum install  --enablerepo=epel socat
then
import os
os.system("echo -en 'READY=1' | socat STDIO ${NOTIFY_SOCKET}")
--------------------------------------------------------------------------------------

Solution 2 in Python
--------------------------------------------------------------------------------------
import os
import socket
env = os.getenv("NOTIFY_SOCKET")
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
sock.connect(env)
sock.send("READY=1")
sock.close()
--------------------------------------------------------------------------------------

It must be applicable to any other languages. The policy is the same. Use socket directly, in stead of using 'systemd-notify' or similar commands/functions.
Reply all
Reply to author
Forward
0 new messages