I’m posing the question in two ways: technically and stylistically.
Can my application/daemon keep a pidfile in /opt/my_app/run/?
Is it very bad to do so?
My requirement is that my daemon runs as a specified user, and the implementor must create a new directory in /var/run, chown, and chgrp it in order for it to execute. It appears to be simpler to keep the pidfile local (to the daemon).
Asked by gmoore
I wouldn’t place a pidfile in the installation directory of a program, such as /opt/my app/whatever. This directory may be read-only, shared between machines, and monitored by a daemon that considers each change as a possible break-in attempt…
/var/run is the standard place for pidfiles. On boot, most unices will clean this directory; in Ubuntu, this is accomplished through the /var/run in-memory filesystem (tmpfs).
Create a subfolder /var/run/gmooredaemon and chown it to the daemon-running user before suing to the user and starting the daemon if you’re launching your daemon from a script that runs as root.
If you start the daemon from a script or launcher that isn’t running as root on many current Linux systems, you can put the pidfile in /run/user/$UID, which is a per-user counterpart of /var/run. The directory must be created by the root component of the launcher, or a boot script executing as root (for a human user, the directory is created when the user logs in).
Otherwise, choose /tmp or /var/tmp, however this adds complication because the pidfile’s name can’t be uniquely identified if it’s in a world-writable directory.
In any event, make it simple for the distributor or administrator to modify the pidfile location (command-line option, or a compile-time option).
Answered by Gilles ‘SO- stop being evil’
The pid file’s location should be adjustable. /var/run is the normal location for pid files, just as /var/log is for logs. Your daemon, on the other hand, should allow you to overwrite this option in a config file.
Answered by Costi Ciudatu
There’s nothing wrong with using /opt to install’self-contained’ apps. Using config files in /opt/my app/etc/, logs in /opt/my app/log/, and so on is standard procedure for this type of application.
This manner, instead of maintaining a package for each package manager, you can distribute your programs as a TGZ file (at least DEB since you tagged ubuntu). This is something I’d advocate for in-house use or scenarios where you have a lot of control over the environment. The logic is that if the safe is more expensive than the contents within, it makes no sense (the work required to pack the application should not eclipse the effort required to write the application).
Answered by Paulo Scardine
If you’re not running the script as root, another convention is to store the pidfile in /.my app/my app.pid. The home directory is not world-writeable, so it’s easier this way while still being secure.
Answered by pestrella
Post is based on https://stackoverflow.com/questions/5173636/must-my-pidfile-be-located-in-var-run