Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

closed as off topic by womble, pauska, Iain May 7 '12 at 5:34

Questions on Server Fault are expected to relate to professional server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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. –  Iain May 7 '12 at 5:34

2 Answers 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.
share|improve this answer

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"
share|improve this answer
    
"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 '12 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.