[systemd-devel] How to compute a value for a service argument or environment value?

Lennart Poettering lennart at poettering.net
Mon Mar 24 16:20:37 PDT 2014


On Thu, 13.03.14 14:37, Alan Stern (stern at rowland.harvard.edu) wrote:

> This question has probably been asked many times before, but I didn't 
> see it mentioned anywhere on the systemd web site.
> 
> I want to create a unit file for a service where the server program
> requires an argument or environment value that has to be computed at
> run time; it isn't known in advance.  For example, suppose the server
> requires the numeric UID value corresponding to some particular
> username, passed as an argument or an environment value.
> 
> In a SysV-type shell script, I could simply do:
> 
> 	exec /path/to/server_program `id -u username`
> 
> or
> 
> 	export USERID=`id -u username`
> 	exec /path/to/server_program
> 
> Neither of these is possible in a systemd service unit file.  So what 
> is the best way to accomplish the same result?
> 
> All I have been able to think of is to have ExecStart= run a shell 
> script that computes the necessary values and then execs the actual 
> server program.  Is there a better way?

If services need such some pre-invocation scripting logic to set up
configuration because they can't do this on their own, I'd always
recommend such a simple wrapper script, just make sure to use "exec" in
the end to invoke the real binary. 

One shouldn't try to shoehorn something where one really would like to
use a real shell script into systemd ExecStart= lines... Of course, I
think it would be best if services can handle their own configuration
directly, but if they can't there's really nothing wrong with wrapping
it in a script that adds that.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list