FPs

踩了Systemd 2个坑

手写Systemd service 的经验不多,非常尴尬,踩了Systemd 2个坑,浪费了近1个小时,记录一下。

Python项目部署使用了virtualenv,添加一个Systemd service,发现启动失败,提示:

File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

忘记设置环境了,EnvironmentFile 里设置PATH

EXAMPLE_ENV=/home/www/example_env
PATH=${EXAMPLE_ENV}:$PATH

发现无论如何都无效,Google一下才发现傻了:systemd: “Environment” directive to set PATH:

You can't use EnvVars in Environment directives. The whole Environment= will be ignored.

改成:

EXAMPLE_ENV=/home/www/example_env
PATH=/home/www/example_env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

终于好了。不过又遇到一个问题,因为:

ExecStart=${EXAMPLE_ENV}/bin/example ....

Systemd 提示:

Executable path is not absolute, ignoring:${EXAMPLE_ENV}/bin/example ...

咦,变量怎么没解析。Google 了下,改成下面这样就好了:

ExecStart=/usr/bin/env ${EXAMPLE_ENV}/bin/example

man/systemd.service

For each of the specified commands, the first argument must be either an absolute path to an executable or a simple file name without any slashes.

2018-08-10 systemd python env