Locating and Managing .pid Files for Process Monitoring in Linux/Python Applications


2 views

.pid files are simple text files containing the process ID (PID) of a running application. They're commonly used by service managers and monitoring tools like monit, supervisord, or systemd to track process states. Not all processes automatically create .pid files - this is typically done by:

  • Daemon processes that implement proper startup scripts
  • Applications launched via init systems
  • Processes explicitly configured to create them

When they exist, .pid files are usually found in these standard directories:

/var/run/      # System-wide daemons
/tmp/         # Temporary process files
/run/         # Modern Linux systems (symlinked to /var/run)
~/.cache/     # User-specific processes
/usr/local/var/run/  # Some manually installed services

To locate a .pid file for a specific process:

# Method 1: Search common locations
sudo find /var/run /tmp /run -name "*.pid" -exec ls -la {} \;

# Method 2: Check process cmdline (if known)
ps aux | grep python
sudo ls -la /proc/[PID]/fd/ | grep pid  # Check file descriptors

For Python apps you want to monitor with monit, explicitly create a .pid file:

import os
import sys
from daemonize import Daemonize  # pip install daemonize

pid_file = "/var/run/myapp.pid"

def main():
    # Your application logic here
    while True:
        pass

if __name__ == "__main__":
    daemon = Daemonize(app="myapp", pid=pid_file, action=main)
    daemon.start()

For non-daemon Python scripts:

import os

pid_file = "/tmp/myscript.pid"

# Create pid file
with open(pid_file, "w") as f:
    f.write(str(os.getpid()))

# Your script logic...

# Optional cleanup
os.remove(pid_file)

Example monit configuration for the Python app:

check process myapp
    with pidfile /var/run/myapp.pid
    start program = "/usr/bin/python3 /path/to/app.py" 
    stop program = "/bin/kill -9 cat /var/run/myapp.pid"
    if cpu > 80% for 5 cycles then alert
    if memory > 500 MB for 3 cycles then restart
  • Always implement proper file locking when creating/writing .pid files
  • Set appropriate permissions (typically root:root or the running user)
  • Clean up .pid files on normal process termination
  • For system services, use your distro's preferred location (/var/run on most systems)
  • Check application logs - many frameworks/logging systems record .pid file locations

In Unix-like systems, a .pid (Process ID) file is a simple text file containing the process ID of a running application. These files are commonly used by process managers like Monit, Supervisord, or systemd to track and manage application states.

There's no universal standard for .pid file locations, but common directories include:

/var/run/
/run/
/tmp/
~/.cache/
Application-specific directories (e.g., /var/log/ for some daemons)

To search for a process's .pid file:

# Search system-wide
sudo find / -name "*.pid" -type f 2>/dev/null

# Check common locations
ls -la /var/run/*.pid /run/*.pid

For your Python application, you can explicitly create a .pid file:

import os
import sys

pid = str(os.getpid())
pidfile = "/tmp/myapp.pid"

if os.path.isfile(pidfile):
    print(f"{pidfile} already exists, exiting")
    sys.exit()
    
with open(pidfile, 'w') as f:
    f.write(pid)

# Your application code here

# Clean up on exit
os.unlink(pidfile)

Not all processes automatically create .pid files. Typically only:

  • Daemons started via init systems
  • Applications explicitly configured to create them
  • Processes managed by supervision tools

If no .pid file exists, you can identify processes by:

# Find process by name
pgrep -f "python myapp.py"

# Or by port
sudo lsof -i :8000

For reliable Monit monitoring:

  1. Choose a consistent .pid file location (/var/run/ preferred for daemons)
  2. Implement proper file cleanup on application exit
  3. Set appropriate file permissions (usually 644)
# Example Monit configuration
check process myapp
    with pidfile "/var/run/myapp.pid"
    start program = "/usr/bin/python /path/to/myapp.py"
    stop program = "/bin/kill -TERM cat /var/run/myapp.pid"