31

I am trying to use the $HOME environment variable in the ExecStart. I tried many different things like $HOME and ${HOME} but nothing seems to be working

ExecStart=${HOME}/bin/some-binary

Anyone knows the correct format for this?

Keeto
  • 441
  • 1
  • 4
  • 6

4 Answers4

31

I think this is what you're looking for: https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers.

Specifically, %h should expand to the current user's home dir.

Jack O'Connor
  • 701
  • 1
  • 7
  • 9
  • According to the documentation you have provided, `%h` is the `user home directory`. But, in my case, `ExecStart` still requested the full path so I ended up typing it starting from the root. – Can Sürmeli Nov 15 '19 at 09:06
  • Using `%h` is probably not what you want. I've posted an alternate answer with details. – pR0Ps Jan 04 '20 at 11:20
13

The full list of supported variables (called "Specifiers") is here: https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Specifiers.

There is no specifier for the home directory of user the service is run as (the one specified by User=). There is only one for the user running the service manager.

From the link:

%h is the home directory of the user running the service manager instance. In case of the system manager this resolves to "/root". Note that this setting is not influenced by the User= setting configurable in the [Service] section of the service unit.

pR0Ps
  • 265
  • 2
  • 7
  • 5
    Just to be clear, that means if I have a user unit, then `%h` most definitely is what I want to use because it will give me the equivalent of `$HOME`. – Cliff Apr 22 '20 at 20:05
  • 1
    Can confirm. %h resolves to $HOME for --user services. – Karl Pokus Apr 24 '21 at 21:21
1

You could use

WorkingDirectory=~

and set relative paths.

crabvk
  • 31
  • 1
0

So, as the absence of practical examples in combination with comments makes a feeling like %h doesn't work (it does actually), here's a full example of a service that starts from HOME dir without hardcoding it.

Given a script ~/test.sh:

#!/bin/bash
echo hello

Creating a file ~/.config/systemd/user/test.service:

[Unit]
Description=Test

[Service]
Type=oneshot
ExecStart=%h/test.sh

[Install]
WantedBy=multi-user.target

then executing a systemctl --user daemon-reload && systemctl --user start test will make it start and print Hello to the journal.

It uses the %h specifier mentioned in other answers and documented as a This is the home directory of the user running the service manager instance. […]. So unless you'll run your user service as another user, this should work.

Systemd versions tested:

  • systemd 253 (253.1-3-arch)
  • systemd 237.
Hi-Angel
  • 277
  • 3
  • 9