Linux - alternative places where to store pid file instead of /var/run

15,521

Solution 1

In short: you could store it anywhere (say, /tmp or /var/tmp), but /var/run is the preferred standard.

/var/run is 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. Programs may have a subdirectory of /var/run; this is encouraged for programs that use more than one run-time file.[footnote 37]

And a desirable feature is that most distros clean it automatically (unlike /tmp which is not cleaned upon boot in some distros) - this avoid stale pid files:

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).

It's your choice where to store it, but I would go with the standard.

If you don't have access to /var/run, you should store the pid file in the user's home directory, e.g. ~/.my_app.pid.

Solution 2

If it's non-root write access to /var/run that you need, then note that there are subdirectories in /var/run/user for individual users. You just need to get the UID of the current user:

/var/run/user/[$uid]

Share:
15,521
Murko
Author by

Murko

Updated on September 18, 2022

Comments

  • Murko
    Murko over 1 year

    As written in the title, where should I let the init script write the pid file? are there any standard paths I should choose instead of the /var/run ?

    Could /tmp be a good place where to store it or there are drawbacks in that?