systemd ExecStartPost doesn't look to be called

I used the following config to start beanstalkd process

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

The last line is supposed to generate a pidfile after the process in launched, but the file is not created. why ?

Or is there another way to force pidfile creation in systemd ?


Solution 1:

systemd does not require a pidfile for a Type=simple service. It will manage the daemon in the foreground. systemctl status SERVICE_NAME will show the pid of the main process (and of any other processes in the cgroup).

For completeness, your ExecStartPost line did not work because systemd does not use a shell to execute commands and does not perform $PATH lookup, so you would have to use ExecStartPost=/bin/sh -c "...", but as I said, the line is unnecessary.

Solution 2:

In case you still need an answer on this (or someone else does), you need a shell context to run pgrep, so the correct command would be

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