What Is a .Pid File and What Does It Contain

What is a .pid file and what does it contain?

The pid files contains the process id (a number) of a given program. For example, Apache HTTPD may write its main process number to a pid file - which is a regular text file, nothing more than that - and later use the information there contained to stop itself. You can also use that information to kill the process yourself, using cat filename.pid | xargs kill

Unix .pid file created automatically?

The latter holds true -- a process must create the .pid file (the OS won't do it). pid files are often used by daemon processes for various purposes. For example, they can be used to prevent a process from running more than once. They can also be used so that control processes (e.g. apache2ctl) know what process to send signals to.

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.

Best practice: PID file for unix daemon

The actual daemon process (the child).

According to the daemon man page provided by the systemd package and viewable on a RHEL 7 (or CentOS 7) host by running man daemon:


  1. In the daemon process, write the daemon PID (as returned by getpid())
    to a PID file, for example /run/foobar.pid (for a hypothetical daemon
    "foobar") to ensure that the daemon cannot be started more than once.
    This must be implemented in race-free fashion so that the PID file is
    only updated when it is verified at the same time that the PID
    previously stored in the PID file no longer exists or belongs to a
    foreign process.

You can also read the man page on the internet.

How to find the pid file of circusd?

If you want to find the pid file , follow the below:

  1. login as root
  2. type the command as below to find all pid files in your server
    find / -name *.pid

Moreover, if you want to check specific process and you don't have it is pid file, you can use matchproc to match the process instead of using it pid file or you have to create a pid file manually .

You can refer to this below example:

Monitor vsftpd

check process vsftpd matching vsftpd
if failed port 21 protocol ftp then alert

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



Related Topics



Leave a reply



Submit