Score:0

How to create a systemd "start all" template unit file from an upstart script with multiple services?

jp flag

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" [email protected]
    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.

Score:0
cz flag

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
DevOpsSauce avatar
jp flag
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 avatar
jp flag
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).
Michael Hampton avatar
cz flag
@IRGeekSauce Well that's a completely unrelated issue.
DevOpsSauce avatar
jp flag
Added `[Install] WantedBy=multi-user.target` and no error that time. Now back to the original task at hand.
DevOpsSauce avatar
jp flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.