Must my pidfile be located in /var/run?
I wouldn't put a pidfile under an application installation directory such as /opt/my_app/whatever
. This directory could be mounted read-only, could be shared between machines, could be watched by a daemon that treats any change there as a possible break-in attempt…
The normal location for pidfiles is /var/run
. Most unices will clean this directory on boot; under Ubuntu this is achieved by /var/run
an in-memory filesystem (tmpfs).
If you start your daemon from a script that's running as root, have it create a subdirectory /var/run/gmooredaemon
and chown it to the daemon-running user before su
ing to the user and starting the daemon.
On many modern Linux systems, if you start the daemon from a script or launcher that isn't running as root, you can put the pidfile in /run/user/$UID
, which is a per-user equivalent of the traditional /var/run
. Note that the root part of the launcher, or a boot script running as root, needs to create the directory (for a human user, the directory is created when the user logs in).
Otherwise, pick a location under /tmp
or /var/tmp
, but this introduces additional complexity because the pidfile's name can't be uniquely determined if it's in a world-writable directory.
In any case, make it easy (command-line option, plus perhaps a compile-time option) for the distributor or administrator to change the pidfile location.
Storing pid file for a daemon run as user
If it's being run for a user, let's see, what sort of storage exists that is user-specific.
Hmmm.
That's it! The home directory. I knew it would come to me eventually :-)
Sorry for the light jab. Seriously, I would just stash the PID into $HOME/.daemon.pid
or ~/.daemon.pid
(how you name the file is up to you of course).
This is, of course, assuming you will only have one daemon running for a user. If not, you'll need to be a bit trickier.
And hopefully allaying your fears that a user will inadvertently delete unknown files in their home directory, that's why you make it "hidden" by starting it with a .
character.
Most non-experienced users should never even see these and experienced users should know better than to muck about with them.
Creating a /var/run folder for a PID file, disappears on reboot?
From the Filesystem Hierarchy Standard:
This directory contains system information data describing the system since it was booted. Files under this directory must be cleared (removed or truncated as appropriate) at the beginning of the boot process.
Your system seems to accomplish this by removing the whole tree (or backing it with a volatile filesystem), which I think is perfectly legitimate.
Preferred location for PID file of system daemon run as non-root user
Nice question :), I'm having exactly the same at moment. I'm not sure if this is the correct answer but I hope it helps and I would appreciate feedback as well.
I've googled around and found that registering the per user daemon as a dbus service is an elegant solution. dbus could make sure that the service runs just once. no need for a pidfile.
Another solution (my current) would be to create the PID file in a directory like:
$HOME/.yourdaemon/pid
After your comment I realized, that you cannot write to home. I would suggest to look into dbus
Update
I have an idea. What if you are using /tmp, but looking for a pidfile which is called yourdaemon.pid.UNIQUE_KEY and is owned by the daemon's user? This should work fine.
UNIQUE_KEY
should be random generated (preferred is using tempnam as it is race condition proof).
Reference for proper handling of PID file on Unix
As far as I know, PID files are a convention rather than something that you can find a respected, mostly authoritative source for. The closest I could find is this section of the Filesystem Hierarchy Standard.
This Perl library might be helpful, since it looks like the author has at least given thought to some issues than can arise.
I believe that files under /var/run are often handled by the distro maintainers rather than daemons' authors, since it's the distro maintainers' responsibility to make sure that all of the init scripts play nice together. I checked Debian's and Fedora's developer documentation and couldn't find any detailed guidelines, but you might be able to get more info on their developers' mailing lists.
MongoDB, an Ubuntu user other than root, and storing the PID file in /var/run/mongodb
Turns out the preferred way to handle startup services in Ubuntu is by using upstart configuration files, which are located in /etc/init
.
Looking at /etc/init/mongodb
, I found a section that looked like this:
pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script
To which I added:
mkdir -p /var/run/mongodb
chown -R mongodb:mongodb /var/run/mongodb
And that seemed to do the trick!
Related Topics
Qt Development on Linux Using Eclipse
Pipe Bash Command Output to Stdout and to a Variable
Best Way Elevate the Privileges Programmatically Under Different Versions of Linux
Bash, Linux, Need to Remove Lines from One File Based on Matching Content from Another File
Git Diff with Line Numbers and Proper Code Alignment/Indentation
How to Solve "Server Terminated Early with Status 127" When Running Node.Js on Linux
How to Use an Older Version of Gcc in Linux
Command Not Found in Bash's If-Else Condition When Using [! -D "$Dir"]
Linux: Modpost Does Not Build Anything
Is There a Special Restriction on Commands Executed by Cron
Where Does Pp (Par) Unpack Add (-A) Files
New Scala Worksheets Not Evaluated in Eclipse
How to Get Details of All Modules/Drivers That Were Initialized/Probed During the Linux Kernel Boot