systemd ExecStartPost看起来不被调用

我使用下面的configuration来启动beanstalkd进程

 [Service] ExecStart=/usr/local/bin/beanstalkd ExecStartPost=pgrep beanstalkd > /var/run/beanstalkd.pid 

最后一行应该是在启动过程后生成一个pidfile,但是文件不会被创build。 为什么?

还是有另一种方法来强制systemid创buildpidfile?

对于Type = simple服务,systemd不需要pidfile。 它将在前台pipe理守护进程。 systemctl status SERVICE_NAME将显示主进程(以及cgroup中的任何其他进程)的pid。

为了完整性,你的ExecStartPost行不起作用,因为systemd不使用shell来执行命令,也不执行$ PATH查找,所以你必须使用ExecStartPost=/bin/sh -c "..." ,但是因为我说,这一行是不必要的。

如果你仍然需要这个(或其他人)的答案,你需要一个shell上下文来运行pgrep,所以正确的命令是

 ExecStartPost=/usr/bin/zsh -c 'pgrep process_name > /var/run/process_name.pid'