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
5

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.

  • Instead of "kill -0" I used /bin/bash -c "[ ! -z $MAINPID ]" – cristi Feb 23 '16 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.

  • I want to control start status even when the daemon is stopped. – cristi Feb 15 '16 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 Feb 15 '16 at 12:50
  • @Cristi the script would have to trap the SIGTERM and kill the real process in function that handles the trap. – Anthon Feb 15 '16 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.