2

I'm trying to rewrite an init.d service file that contains the following code:

    if [ ! -r /var/spool/torque/server_priv/serverdb ]; then
            DAEMON_SERVER_OPTS="-t create $DAEMON_SERVER_OPTS"
    fi
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_SERVER_OPTS

I have rewritten this as follows:

[Service]
Type=forking
ExecStart=/usr/sbin/pbs_server
PIDFile=/var/spool/torque/server_priv/server.lock

But I have no idea how to represent the if in systemd.

  • Could you have the server itself do the check, or have a wrapper script to do that check and add the extra options instead of trying to do it in the service file directly? – Eric Renouf Aug 18 '15 at 14:14
2

As it's already been said, there is deliberately no support for complex logic in systemd. If there is any start-up logic to do (and it isn't part of the daemon itself), it is completely OK to write a small shell script and specify it in ExecStart=.

There is one thing to consider, though. The shell script must not do any process management by itself. The shell script must exec the daemon. This is required to avoid interference with systemd's own process watching and management.

An example of a wrong shell script:

#!/bin/sh
if [ ! -r /var/spool/torque/server_priv/serverdb ]; then
        DAEMON_SERVER_OPTS="-t create $DAEMON_SERVER_OPTS"
fi

$DAEMON -- $DAEMON_SERVER_OPTS

This makes the daemon a child of the shell interpreter. If the daemon does not fork and the readiness protocol (Type=) is simple, then it's just a redundant process hanging around. Otherwise, if the daemon forks and you set Type=forking, then the whole thing will triple-fork, not double-fork, and systemd will kill the daemon.

An example of a correct shell script:

#!/bin/sh
if [ ! -r /var/spool/torque/server_priv/serverdb ]; then
        DAEMON_SERVER_OPTS="-t create $DAEMON_SERVER_OPTS"
fi

exec $DAEMON -- $DAEMON_SERVER_OPTS

This replaces the shell process with the daemon.

3

You are not going to like this, but it works:

ExecStart=/bin/bash -c '\
    if [ ! -r /var/spool/torque/server_priv/serverdb ]; then \
            DAEMON_SERVER_OPTS="-t create $DAEMON_SERVER_OPTS"; \
    fi; \
    exec /usr/sbin/pbs_server -- $DAEMON_SERVER_OPT'
0

There's no such facility in systemd - because this sort of logic really belongs inside of daemon. However if you can't change the daemon in question you can workaround it by first creating simple unit which will check file existence and create corresponding environment variable and than add "EnvironmentFile=" to your unit. Also add dependency to make sure that your unit is always executed after the one which creates environment variables.

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.