You are not logged in.

#1 2016-01-12 23:06:39

dominicm
Member
Registered: 2015-01-03
Posts: 92

Systemd fails with tilde present ~/ instead of /home/user

I have a simple systemd script:

[Unit]
Description=test
After=local-fs.target network.target

[Service]
Type=simple
ExecStart=/usr/bin/test --config ~/.config
Restart=no

[Install]
WantedBy=multi-user.target

Replacing ~/.config with /home/user works just fine. I also tried $HOME instead of ~ with no luck. What am I missing here?

Last edited by dominicm (2016-01-12 23:33:59)

Offline

#2 2016-01-12 23:12:54

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Systemd fails with tilde present ~/ instead of /home/user

It runs as root.  I'm not sure if ~ is valid outside a shell, but if it is, it points at /root.
If you are having root run something in a user directory that is writable by that ordinary user, you are opening a large gaping security hole.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#3 2016-01-12 23:31:55

dominicm
Member
Registered: 2015-01-03
Posts: 92

Re: Systemd fails with tilde present ~/ instead of /home/user

I forgot to mention that the script is started as the current non-root user:

systemctl --user test.service

Also the systemd script is located in ~/.config/systemd/user

The command in ExecStart works fine when executed manually by the same user.

Last edited by dominicm (2016-01-12 23:35:06)

Offline

#4 2016-01-12 23:35:24

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Systemd fails with tilde present ~/ instead of /home/user

systemd is NOT bash, you cannot expect bash substitution, variables, tilde expansion, etc. to work in a systemd unit file.

See Systemd specifiers for the correct way to do this.

Last edited by eschwartz (2016-01-12 23:36:20)


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2016-01-12 23:44:47

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: Systemd fails with tilde present ~/ instead of /home/user

dominicm wrote:

I forgot to mention that the script is started as the current non-root user:...Also the systemd script is located in ~/.config/systemd/user.

That it is the copy that is running.  But Eschwartz is right.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#6 2016-01-14 12:29:54

dominicm
Member
Registered: 2015-01-03
Posts: 92

Re: Systemd fails with tilde present ~/ instead of /home/user

Eschwartz wrote:

systemd is NOT bash, you cannot expect bash substitution, variables, tilde expansion, etc. to work in a systemd unit file.

See Systemd specifiers for the correct way to do this.


%h/test worked, thank you. Are systemd specifiers the only variables that will work in systemd scripts? Where would $HOME be used?

Offline

#7 2016-01-15 21:02:44

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: Systemd fails with tilde present ~/ instead of /home/user

Hm, sort of.

http://www.freedesktop.org/software/sys … nd%20lines

The rules are complicated, whitespace splitting is not entirely intuitive from a bash perspective, only direct "variable=definition" assignment is supported, and you are encouraged to treat systemd unit files as a brand new language. smile


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#8 2016-01-15 21:12:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,571
Website

Re: Systemd fails with tilde present ~/ instead of /home/user

I think this is already covered above, but to make it explict, when one types "/usr/bin/test --config ~/.config" at a command prompt, it is the shell interpreter that expands this to "/usr/bin/test --config /home/username/.config" before the 'test' binary is even called.  Then the 'test' binary is called (via an exec family function) with three parameters:

argv[0]=/usr/bin/text
argv[1]=--config
argv[2]=/home/username/.config

When you pass this same thing to the Exec line of a systemd unit file it is not parsed and expanded by a shell, but instead is passed directly to an exec family function which calls the 'test' binary with the following three parameters:

argv[0]=/usr/bin/text
argv[1]=--config
argv[2]=~/.config

If /usr/bin/test is a shell script it could expand the tilde itself, but if it is a binary, it will look for a file literally named "~/.config".



In principle one could get around this with the following:

ExecStart=/usr/bin/bash -c /usr/bin/test --config ~/.config

But there are better ways suggested above.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2016-01-15 22:03:12

branch
Member
Registered: 2014-03-16
Posts: 209

Re: Systemd fails with tilde present ~/ instead of /home/user

If this is a --user service consider setting WantedBy=default.target as multi-user.target is a system target.

Offline

#10 2016-01-15 22:21:12

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Systemd fails with tilde present ~/ instead of /home/user

branch wrote:

[..] multi-user.target is a system target.

As are local-fs.target and network.target.

And if I'm reading those man pages right, ${HOME} would have worked (with curly braces).

Offline

#11 2016-01-16 13:46:02

ayekat
Member
Registered: 2011-01-17
Posts: 1,634

Re: Systemd fails with tilde present ~/ instead of /home/user

Trilby wrote:

If /usr/bin/test is a shell script it could expand the tilde itself, but if it is a binary, it will look for a file literally named "~/.config".

Nit-picking: If the shell script is properly written, it should quote strings (especially command line arguments), where ~ would no longer expand to $HOME. So, in a just world, ~ will cause problems.


pkgs │ hacks │ cfg │ blag

Offline

#12 2016-01-16 14:08:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,571
Website

Re: Systemd fails with tilde present ~/ instead of /home/user

That's why I said "could" not "will" wink


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB