18

I'm setting up monit and want to monitor a given python application. Monit does this by looking at the .pid files for processes, but I don't know where this would be.

I also tried creating my own simple executable and running it- here too I can't figure out where the .pid file is created.

And do all processes have a .pid file?

Yarin
  • 1,316
  • 8
  • 19
  • 31
  • 6
    Not all processes have a `.pid` file. The application (or its start up script) needs to explicitly create one. – bahamat Aug 15 '12 at 18:13

2 Answers2

17

You'll usually find the PID files for daemonized processes in /var/run/ on Redhat/CentOS-style systems.

Short of that, you can always look in the process init script. For instance, the SSH daemon is started with the script in /etc/init.d/sshd. Sometimes the PID will be defined there (search for pid, PID, PIDFILE, PID_FILE, etc.).

However, most other daemons on RHEL-style systems source the /etc/init.d/functions script for some common features.

# Set $pid to pids from /var/run* for {program}.  $pid should be declared
# local in the caller.
# Returns LSB exit code for the 'status' action.
__pids_var_run() {
        local base=${1##*/}
        local pid_file=${2:-/var/run/$base.pid}

For anything that sources /etc/init.d/functions, the PID will live in /var/run/*.pid.

For custom applications, the PID will be defined in a wrapper script (hopefully). Most developers I know follow the same convention as the daemons above, though.

If you do encounter something without a PID file, remember that Monit can monitor on a process string patern as well.

ewwhite
  • 194,921
  • 91
  • 434
  • 799
  • 1
    ewwhite- Thanks a lot- but when I try `monit procmatch anything` on the commandline I get `monit: invalid argument -- procmatch`. Any ideas? – Yarin Aug 15 '12 at 18:36
  • What version of Monit are you using? (Type `monit -V`) Which OS/distribution? – ewwhite Aug 15 '12 at 18:43
  • Monit 5.1.1 yummed onto CentOS 6 – Yarin Aug 15 '12 at 18:45
  • Also, what would be my options for multiple non-daemon processes? What do you think about [this answer](http://serverfault.com/a/288773/41823) – Yarin Aug 15 '12 at 18:50
  • @yarin It looks like you have the Monit from the [EPEL repository](http://fedoraproject.org/wiki/EPEL). The version for EL6 from [RPMForge](http://repoforge.org/) is 5.4. – ewwhite Aug 15 '12 at 18:52
  • Thanks- changed repos and upgrades- procmatch works. To confirm though, this would only be for single, daemonized processes- correct? – Yarin Aug 15 '12 at 19:05
  • It only applies to the first matching process of a particular string. – ewwhite Aug 15 '12 at 19:05
2

Another approach I took:

I have a database server running in embedded mode, and the data are within the containing application's directory.

The database has something like a .pid file, but it calls it lock file. To locate this lock file, I listed all files held open by the app:

$ ls -l /proc/18264/fd | cut -d'>' -f2

That gave me a long list including sockets, pipes, server files etc. Few filters and I got to what I needed:

$ ls -l /proc/18264/fd | cut -d'>' -f2 | grep /home/ | cut -b40- | sort | uniq | grep titan

/windup/reports/group_report.LJfZVIavURqg.report/graph/titangraph/00000000.jdb
/windup/reports/group_report.LJfZVIavURqg.report/graph/titangraph/je.info.0
/windup/reports/group_report.LJfZVIavURqg.report/graph/titangraph/je.info.0.lck
/windup/reports/group_report.LJfZVIavURqg.report/graph/titangraph/je.lck
Ondra Žižka
  • 424
  • 2
  • 5
  • 14