systemd - Giving my service multiple arguments
Solution 1:
Yes you can! Define them in a file somewhere and add them to EnvironmentFile
in your systemd Service. For example, say the contents of /etc/.progconf are:
ARG1=-o
ARG2=--verbose
And your .service file:
EnvironmentFile=/etc/.progconf
ExecStart = /usr/bin/prog $ARG1 $ARG2
You can write to that file if you need to change them on the go. A service shouldn't change its options very often, maybe consider autostarting or cron if you need to achieve that.
For more examples check: https://wiki.archlinux.org/index.php/Systemd/Services
Solution 2:
I wanted to do the same thing, but without a separate file for each combination of arguments. I found that I could pass one long argument with spaces and then use systemd's environment variable space-splitting feature to separate the arguments.
I made a service with filename [email protected]
(note the trailing 'at sign' which is required when a service takes arguments).
[Unit]
Description=Test passing multiple arguments
[Service]
Environment="SCRIPT_ARGS=%I"
ExecStart=/tmp/test.py $SCRIPT_ARGS
I run this with sudo systemctl start argtest@"arg1 arg2 arg3".service
and it passes arg1
, arg2
and arg3
as separate command-line arguments to test.py.
Solution 3:
Easiest I have found is:
ExecStart=/bin/bash -c "\"/path/with spaces/to/app\" arg1 arg2 arg3 \"arg with space\""
Keeps it all self contained.
Having said that, I have found that at least on Ubuntu 18.04 LTS, I don't even need to do that, I can do this and it works fine:
ExecStart="/path/with spaces/to/app" arg1 arg2 arg3 "arg with space"
$vars
work as arguments with this pattern as well.