Aug 25, 2018

systemd: systemd-notify not working for non-root-users

Sometimes you have to write your own startup scripts. Recent linux distributions require systemd scripts. This is not really a problem except you have to fulfill the following requirements:
  • Run the service as a non-root-user
  • The service has a startup phase and you want to start the next startup scripts after this startup phase
So the systemd-script has to look like this:
# cat /lib/systemd/system/TEST.service
[Unit]
Description=MyTestSystemdConfiguration

[Service]
User=schroff
Type=notify
ExecStart=/home/schroff/bin/test.sh
NotifyAccess=all
The service startup scripts have to look like this:
$ cat /home/schroff/bin/test.sh
#!/bin/bash

echo Starting serivce
sleep 10
#Starting your services
echo Services started

/bin/systemd-notify --ready
echo Notify done

while test 1 do
  sleep 600
done
#keep this scripts running, as long your service runs
In the startup phase you will get the following:
schroff@zerberus:~/bin$ systemctl status TEST.service
● TEST.service - MyTestSystemdConfiguration
   Loaded: loaded (/lib/systemd/system/TEST.service; static; vendor preset: enabled)
   Active: activating (start) since 19:39:27 CET; 7s ago
 Main PID: 17390 (test.sh)
    Tasks: 2 (limit: 4915)
   Memory: 532.0K::
      CPU: 7ms
   CGroup: /system.slice/TEST.service
           ├─17390 /bin/bash /home/schroff/bin/test.sh
           └─17395 sleep 10

19:39:27 zerberus systemd[1]: Starting MyTestSystemdConfiguration...
19:39:27 zerberus test.sh[17390]: Starting serivce
And after the startup phase this is the output (if there were no errors):
schroff@zerberus:~/bin$ systemctl status TEST.service
● TEST.service - MyTestSystemdConfiguration
   Loaded: loaded (/lib/systemd/system/TEST.service; static; vendor preset: enabled)
   Active: active (running) since 19:38:38 CET; 3s ago
 Main PID: 17242 (test.sh)
    Tasks: 2 (limit: 4915)
   Memory: 932.0K
      CPU: 9ms
   CGroup: /system.slice/TEST.service
           ├─17242 /bin/bash /home/schroff/bin/test.sh
           └─17259 sleep 600

19:38:28 zerberus systemd[1]: Starting MyTestSystemdConfiguration...
19:38:28 zerberus test.sh[17242]: Starting serivce
19:38:38 zerberus test.sh[17242]: Services started
19:38:38 zerberus systemd[1]: Started MyTestSystemdConfiguration.
19:38:38 zerberus test.sh[17242]: Notify done
But sometime you will get:
# systemctl restart TEST.service
Job for TEST.service failed because a timeout was exceeded.
See "systemctl  status TEST.service" and "journalctl  -xe" for details.
19:44:46 zerberus systemd[1]: TEST.service: Start operation timed out. Terminating.
19:44:46 zerberus systemd[1]: Failed to start MyTestSystemdConfiguration.
-- Subject: Unit TEST.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit TEST.service has failed.
--
-- The result is failed.
19:44:46 zerberus systemd[1]: TEST.service: Unit entered failed state.
19:44:46 zerberus systemd[1]: TEST.service: Failed with result 'timeout'.

Note that this will happen after 600s (default). You can change this with the parameter (systemd configuration, see manpage systemd.service)
TimeoutSec
But changing this Parameter will not help, because systemd status will never enter the state "active (running)".

The problem is systemd-notify doesn't work, since it lives too short (Redhat Bugzilla).


A workaround is described in that bug entry:
Instead of
systemd-notify --ready
use
python -c "import systemd.daemon, time; systemd.daemon.notify('READY=1'); time.sleep(5)"

No comments:

Post a Comment