How to escape spaces in systemd unit files?
14,696
Solution 1
The correct way to generate paths in systemd is to use systemd-escape.
i.e.
~$ systemd-escape --path "/home/cobra/my service/start.sh"
home-cobra-my\x20service-start.sh
Yes /
gets replaced with -
Solution 2
The obvious thing to do is to use double quotes.
ExecStart="/home/cobra/my service/start.sh"
You also should get rid of the start.sh script and move any necessary logic into the unit.
Related videos on Youtube
Author by
Cobra_Fast
Updated on September 18, 2022Comments
-
Cobra_Fast over 1 year
My unit file looks like this (already attempted to escape spaces as
\x20
like the docs say):[Unit] Description=My Service [Service] Type=simple WorkingDirectory=/home/cobra/my\x20service/ ExecStart=/home/cobra/my\x20service/start.sh [Install] WantedBy=multi-user.target
but when attempting to start it, it fails with the following message:
Failed at step CHDIR spawning /home/cobra/my service/start.sh: No such file or directory myservice.service: main process exited, code=exited, status=200/CHDIR
Giving the path from this error message to
stat
returns:File: ‘/home/cobra/my service/start.sh’ Size: 280 Blocks: 8 IO Block: 4096 regular file Device: 903h/2307d Inode: 4718912 Links: 1 Access: (0754/-rwxr-xr--) Uid: ( 1000/ cobra) Gid: ( 1000/ cobra) Access: 2015-05-24 22:42:12.702657594 +0200 Modify: 2015-03-27 22:28:05.682531000 +0100 Change: 2015-05-24 22:40:58.830298787 +0200 Birth: -
I cannot remove the spaces from the file name as the service I'm attempting to run requires them for some reason.
Where am I going wrong?
-
hookenz almost 9 yearsHave you tried backslash space ("\ ") like the shell?
-
hookenz almost 9 yearsIt seems that spaces are used as a way of separating arguments. And googling this returns some ugly workarounds. Can you change the path? otherwise what about trying a non-space separated symbolic link as the path name which points to the real directory?
-
hookenz almost 9 yearsWell I did find a tool called systemd-escape which allows you to create paths. It seems / gets changed to dash.
-
leetNightshade almost 4 yearsDid you figure out how to get WorkingDirectory to work with spaces? Nothing is working for me.
-
-
Cobra_Fast almost 9 yearsPutting quotes around it like you suggested makes the service fail completely:
Failed to start myservice.service: Unit myservice.service failed to load: Invalid argument.
-
Cobra_Fast almost 9 yearsThe underlying error message seems to be
Executable path is not absolute, ignoring: "/home/cobra..."
myserivce.service lacks ExecStart setting. Refusing.
-
Cobra_Fast almost 9 yearsTurns out quoting the executable path is supported since systemd version 218-147 (2014-12-19) and debian jessie is still sitting on 215-17. I just wrote the debian people a bugreport, we'll see what comes of it.
-
Michael Hampton almost 9 years@Cobra_Fast Good luck with that. Of course I would never advise using Debian at all, but that's another discussion...
-
intcreator over 2 yearsin my case I only needed to escape the path for the
WorkingDirectory
setting using thesystemd-escape
command before my service worked. is it best practice to escape paths used in theExecStart
setting with the command as well or does is it fine to just use quotes as long as it works? -
hookenz over 2 yearsI would use quotes if possible. But failing that, this tool. In the end you need to try something that works. In my opinion, systemd-escape is a funny tool that produces pretty odd looking escape sequences (dash for slash?? etc). The systemd team made some odd choices down the road. This is one of them.
-
Dominik over 2 yearsIn my case, paths returned by systemd-escape did not work. Replacing spaces with \x20 and no quotes worked.