1

How can I create a pid file for a C program to be used in monit, and how do I monitor that C program using monit?

1
  • Welcome to Server Fault! This question is off topic for serverfault. It may be on topic for Stackoverflow but check their faq and search their site before asking there.
    – user9517
    May 7, 2012 at 5:34

2 Answers 2

2

Alternate solution: don't use monit. Writing daemons properly is hard, and in my experience it's a whole lot easier just to use a process management system that does the daemonisation for you (I like daemontools -- it's not the only option out there, but I've tried 'em all and I keep coming back to daemontools). The benefits of doing things that way are many:

  • Daemonising is hard: Properly daemonising a program is hard (not "multi-threaded" hard, but at least "you will get it horribly wrong the first 20 times" hard). Why waste your time on writing (and frequently debugging) code that doesn't add business value, when someone else has already done it for you?
  • PID files suck: They're racy (there is a period of time when the process is running but the PID file isn't), not guaranteed to be accurate (when your daemon dies and another process takes it's PID, it makes things so much more complicated), and they require filesystem access (possibly to places you'd rather not let unprivileged programs have access to).
  • It's not monit: I spent 8 months of my life being kicked in the head by monit and it's many ideosyncracies, design flaws, bugs, and miscellaneous insanity. I will leave a job rather than use monit again -- I feel that strongly about it.
2

I think Monit is still a useful tool. There aren't many solid alternatives.

As for the C program, you should really look into daemonizing the process. That is the proper method. Monit is capable of monitoring process name patterns instead of PID files. In a pinch, you can have a Monit setup that looks like the following, assuming the name of the program is "c_program" and that there's a single instance of it:

check process c_program
        matching "c_program"
        start program = "/path/to/c_program"
        stop program = "killall c_program"
1
  • "There aren't many solid alternatives" -- Monit isn't a solid alternative. Also, monitoring process name patterns is even less reliable than PID files, since you can never guarantee there'll only be a single instance of a program.
    – womble
    May 6, 2012 at 11:13

Not the answer you're looking for? Browse other questions tagged .