2

I have a systemd service (for heka) which causes me some headaches.

The problem is that "start" returns successfully even if the heka daemon dies shortly after starting. This is happening if the configuration files are wrong, for example: the process will start, it will verify the configuration and die if it's not happy about what it finds. Systemd returns successfully in this case.

Is there any way to force systemd to check the program status after it is initializing? Maybe to sleep n seconds after the process has started?

This is the script:

    [Unit]
    Description=Heka event/metric/log collection and routing daemon
    After=network.target auditd.service
    ConditionPathExists=!/etc/heka/hekad_not_to_be_run

    [Service]
    EnvironmentFile=-/etc/default/heka
    Type=simple
    PIDFile=/var/run/hekad.pid
    ExecStart=/usr/bin/hekad -config=/etc/heka
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=process
    Restart=on-failure
    StandardError=inherit

    [Install]
    WantedBy=multi-user.target
    Alias=heka.service

3 Answers 3

7

You can chain multiple ExecPostStart commands together. And you can run them even if the main ExecStart failed by adding a -/ (systemd.service: Type=). Something like this:

ExecStart=-/usr/bin/hekad -config=/etc/heka
ExecStartPost=/bin/sleep 3
ExecStartPost=/bin/kill -0 $MAINPID &>/dev/null

This ensures that you still have the MAINPID to use when stopping or restarting the service for instance.

1
  • Instead of "kill -0" I used /bin/bash -c "[ ! -z $MAINPID ]"
    – cristi
    Commented Feb 23, 2016 at 10:38
0

If there is an easy way to check whether hekad is still alive then I would start the daemon using ExecStartPre=/usr/bin/hekad -config=/etc/heka and with ExecStart run a small script that has a loop that first sleeps X seconds, then checks if the hekad daemon is still running. Breaking the loop only if the hekad seems to have stopped.

3
  • I want to control start status even when the daemon is stopped.
    – cristi
    Commented Feb 15, 2016 at 12:42
  • Sorry,I didn't read your answer correctly the first time. But what happens when you want to stop with your script? Systemd will send the kill signals to the process from ExecStart.
    – cristi
    Commented Feb 15, 2016 at 12:50
  • @Cristi the script would have to trap the SIGTERM and kill the real process in function that handles the trap.
    – Anthon
    Commented Feb 15, 2016 at 15:49
0

You should use ExecStartPost to launch a command or script that sleeps and then runs a check. If this exits non-zero, the unit is considered a failure.

See: https://www.freedesktop.org/software/systemd/man/systemd.service.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .