You are not logged in.
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.targetReplacing ~/.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
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
I forgot to mention that the script is started as the current non-root user:
systemctl --user test.serviceAlso 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
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
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
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
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. ![]()
Managing AUR repos The Right Way -- aurpublish (now a standalone tool)
Offline
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
If this is a --user service consider setting WantedBy=default.target as multi-user.target is a system target.
Offline
[..] 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
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.
Offline
That's why I said "could" not "will" ![]()
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline