Is there a way to dynamically assign environment variables in a systemd service unit file?
We have a machine that has 4 GPUs, and we want to spin up multiple instances of a certain service per GPU. E.g.:
- gpu_service@1:1.service
- gpu_service@2:1.service
- gpu_service@3:1.service
- gpu_service@4:1.service
- gpu_service@1:2.service
- gpu_service@2:2.service
- gpu_service@3:2.service
- gpu_service@4:2.service
- ad nauseam
So the 1:1, 2:1, etc. are effectively the %i in the service unit file.
In order for the service to bind to a particular GPU, the service executable checks a certain environment variable, e.g.:
USE_GPU=4
Is there a way I can take %i inside the service unit file and run it through some (shell) function to derive the GPU number, and then I can set the USE_GPU environment variable accordingly?
Most importantly, I don't want the hassle of writing multiple /etc/systemd/system/gpu_service@x:y.service/local.conf
files just so I can spin up more instances.