I'm trying to convert an old init script into a systemd unit file.

My init script sources a configuration file and depending on some of the variables, some arguments are added to the daemon.

The init script does something similar to this:

## My daemon
DAEMON="/usr/bin/my_daemon"

## These arguments are fixed:
DAEMON_ARGS="-a -b -c"

# Called on 'service my_service start'
do_start() {
    ## Source config file
    . /etc/me/config_file

    ## If FOO is defined, add -x $FOO to arguments
    if [ -n "$FOO" ]; then
        DAEMON_ARGS="$DAEMON_ARGS -x $FOO"
    fi

    ## Start the daemon
    start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_ARGS
}

How can I achieve the same effect with a unit file?

Edit 1:

Here's a skeleton unit file:

[Unit]
Description=My service
Wants=local-fs.target
Requires=local-fs.target

[Service]
PIDfile=/var/run/daemon.pid
Type=forking
ExecStart=/usr/bin/my_daemon -a -b -c

[Install]
RequiredBy=basic.target
WantedBy=basic.target

I want to pass the argument -x $FOO to the daemon in ExecStart, but ONLY if $FOO is defined in some configuration file (let's say, /etc/me/config_file).

I know I can read an EnvironmentFile, so I could do:

[Service]
EnvironmentFile=/etc/me/config_file
ExecStart=/usr/bin/my_daemon -a -b -c -x $FOO

But FOO might not be defined, so the -x argument will have no value... and an error will happen.

$FOO is an option that is read by other services, so it is not viable to make FOO=-x <value>.

  • 1
    I would start by looking at some existing unit file, maybe in `/usr/lib/systemd/system', and the systemd.unit manual page. From there you might figure it out or at least be able to put together a skeleton of your unit file for everyone to look at and possibly help you out from there. – airhuff Jan 20 '17 at 20:53
  • I edited my question. I have looked at other unit files. Maybe I missed something, but I didn't see a way to do what I want. And sorry if I didn't explain myself clearly. – user5728855 Jan 20 '17 at 21:11
  • Just put all of your options into /etc/me/config_file like OPTIONS="-a -b -c -x <foo>" and use ExecStart=/usr/bin/my_daemon ${OPTIONS} to start the service. – Thomas Jan 21 '17 at 11:11
  • I've been fighting this for almost a week now and I can't find any solution other than either hard coding the daemon arguments as suggested by Thomas or just giving up and using a shell script. It seems strange that the relatively common pattern of appending arguments to a variable like $DAEMON_ARGS above isn't supported by systemd. A shame really. My first real contact with systemd did not impress me. – user5728855 Jan 26 '17 at 18:26
  • I've been frusted by similar limitations of systemd. My suggestion is to modify my_daemon (it is yours, right?) to accept -x= or -x=$FOO style options, so your service can read ExecStart=/usr/bin/my_daemon -a -b -c -x=$FOO. – Mark Lakata Jul 29 '17 at 2:13

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.