1

I have made a python script to process some data that I want to run as a service. The script is processing the data, then sleeping for a while to process new data that has been coming in.

My problem is that I want to run several instances of the service as I have a few different data source that I want to process and my script is made to handle one data source at the time.

I see a few different ways of doing this:

I could rewrite the script to handle all the data sources - this will make it unusable for ad hoc use and will also make it more difficult to update the set of data sources as it is not possible to autodiscover sources that have been added or removed, so even though it is possible, this is my least favorable option.

I could make a wrapper script in bash or python that starts scripts for the data sources I want to process. This also feels a bit kludgy.

I could make one unit file for each data source, each calling the processing script with the appropriate arguments, this will give me a number of system files, i.e. processing_ds1.system, processing_ds2.system etc. To me this feels like a possible way, unless there are some good reason not to do it.

My preferred way would be to make one system file that either can be called with arguments for each data source or starting all the instances within the system file.

So I have two alternatives:

How is it possible to use arguments with unit files? I have seen postgresql can start different versions using @, i.e

systemctl start postgres@12-main

to start the cluster 12-main. But how is this done? I am not able to find any maningful unit file for postgresql.

Or

Is it possible and adviceable to start multiple services from one unit file?

4
  • freedesktop.org/software/systemd/man/systemd.service.html is a good starting point.
    – Panki
    Mar 30, 2020 at 9:04
  • Are you sure it's worth doing as a service? I mean, what you describe is trivial in anything but a service, and I am having trouble thinking of a usecase where the service would be helpful. Is running it as a regular script instead an option? If not, what is it that makes you require a service? Perhaps we can find a way around that.
    – terdon
    Mar 30, 2020 at 10:25
  • I want it to run continously, being automatically started as a specific user (not me) when the PC starts and being automatically restarted if it should for some reason stop.If you know some easier way of doing that than writing a unit file, I am all ears. What would the problem be running it as a service? Mar 30, 2020 at 10:29
  • The simpler solution would be to write a script that does the job you want and then exits... then create a service with Type=oneshot and create a systemd timer unit to start that service on the desired schedule. I believe this is what @terdon is getting at. This also works just as well with templated services as describe in @kurcze's answer below. Trying to get a script to run forever is almost always the wrong approach. Apr 1, 2020 at 18:33

1 Answer 1

4

You can use a template unit file: systemd.unit

For OpenVPN as an example: You can create a unit file with a name [email protected] in /etc/systemd/system with a following content:

[Unit]
Description=OpenVPN connection to %i
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
ExecStart=/usr/sbin/openvpn --config /etc/openvpn/%i.conf
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target

Every time you run systemctl start openvpn@myconfig systemd is going to substitute %i in the unit file for "myconfig". So you run /usr/sbin/openvpn --config /etc/openvpn/myconfig.conf

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .