0

I'm in the process of migrating all custom upstart scripts to systemd. I've come across a script that utilizes multiple services. I cannot figure out the proper syntax to handle this, or if I need to just create separate .service unit files for each. Is this possible for templating? The SystemD Unit Documentation doesn't give me much information, except for how to create a template file (appending @ to the name), and how to use %i to signify an instance.

The original upstart dealer-start-all.conf

console log
start on dealer-start
script
    declare -a dealers=("TimeZone" "Timeout" "Inquiry" "Refuse")

    for type in "${dealers[@]}"
    do
        if initctl list | grep "^dealer ($type)"
        then
            stop dealer type=$type
        fi
        start dealer type=$type
        echo "dealer$type started"
    done
end script

The other part of it, dealer.conf, should be pretty cut and dry by using %i in the ExecStart portion, like:

ExecStart=/usr/bin/php -f /path/to/dealer%i.php

console log

instance $type

stop on dealer-stop

script
        sudo -u root php -f /path/to/dealer$type.php
end script

post-stop script

if [ -z "$UPSTART_STOP_EVENTS" ]
    then
        echo "dealer$type stopped at `date +"%F %T.%N"` Run 'initctl emit dealer-stop' then 'initctl emit dealer-start' on `hostname` to get it running again." | mail -s "dealer$type Stopped" alerts@myemail.com
    else
        echo "dealer$type was manually stopped at `date +"%F %T"`."
fi
end script

I just don't understand how to translate the array in the first one into a systemd version? Should I break these up into individual unit files? If so, then that's not a problem and can be easily done. I'm just unsure of syntax (if it exists) to do what the first one is doing.

DevOpsSauce
  • 288
  • 4
  • 13

1 Answers1

0

The systemd unit template is a template. You aren't going to put the array in it. Rather you're going to instantiate it for each instance you want, e.g.:

systemctl enable dealer@TimeZone
systemctl enable dealer@Timeout
...

Where %i appears in the template will be replaced with what you specified.

You also can't use %i in the binary name in ExecStart=. It must be a path that exists, and %i used in its arguments. For example:

ExecStart=/usr/bin/php -f /path/to/dealer%i.php
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
  • Thank you. I had the /usr/bin/php portion, but forgot to type it in my question. I'll try this out and come back to accept. – DevOpsSauce Jul 02 '21 at 16:02
  • I tried to enable one with `systemctl enable dealer@TimeZone`, and received an error: "The unit files have no installation config (WantedBy, RequredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units). – DevOpsSauce Jul 02 '21 at 17:23
  • @IRGeekSauce Well that's a completely unrelated issue. – Michael Hampton Jul 02 '21 at 17:24
  • Added `[Install] WantedBy=multi-user.target` and no error that time. Now back to the original task at hand. – DevOpsSauce Jul 02 '21 at 18:35
  • I got that to work. I added a `dealers.target` file with `Requires=dealer-names-here`. Still not completely sure about all this, but this at least answered by initial question. Thank you for your help. – DevOpsSauce Jul 02 '21 at 19:03