如何逃避systemd单元文件中的空格?

我的单元文件看起来像这样(已经试图像文档所说的那样将空格转义为\x20 ):

 [Unit] Description=My Service [Service] Type=simple WorkingDirectory=/home/cobra/my\x20service/ ExecStart=/home/cobra/my\x20service/start.sh [Install] WantedBy=multi-user.target 

但试图启动时,会失败并显示以下消息:

 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 

从这个错误信息给stat返回的path:

  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: - 

我无法从文件名中删除空格,因为我试图运行的服务因为某些原因需要它们。

我哪里错了?

在systemd中生成path的正确方法是使用systemd-escape。

 ~$ systemd-escape --path "/home/cobra/my service/start.sh" home-cobra-my\x20service-start.sh 

/被replace为-

显而易见的事情是使用双引号。

 ExecStart="/home/cobra/my service/start.sh" 

你也应该摆脱start.sh脚本,并将任何必要的逻辑移入单元。

对于ExecStart中的空格,有一个开放的错误报告。[1] 解决方法是使用/usr/bin/env后跟引号中的path。 例:

ExecStart=/usr/bin/env "/path/with spaces/executable"

规范但不是很好的解决scheme是使用systemd-escape

systemd-escape --path "/path/with spaces/executable"

[1] https://github.com/systemd/systemd/issues/2132