Having written several small daemons or web applications for my personal use, I like to manage them using systemd (I am using Arch Linux, if that matters). So I write service files like the one below:
[Unit] Description=My example daemon [Service] Type=forking User=example_user ExecStart=/path/to/my_executable ExecStop=kill -QUIT $MAINPID PIDFile=/var/run/example/example.pid [Install] WantedBy=multi-user.target
my_executable fails to start if it cannot write its PID file, which generally means that the directory
/var/run/example/ does not exist. This happens after every reboot since I have
/var/run of a tempfs.
One solution is to start my_executable as root and have it drop to the correct access rights after creating its environment, but I not really comfortable with that, since I don't trust myself to do it correctly.
Is there a way to make systemd do the heavy lifting here (maybe by specifying another script that should run before doing ExecStart)? What is the best way?