Using Monit to monitor custom daemons
Asked Answered
F

1

6

I have a daemon written in PHP. I use the following command to call the daemon

php dojobs.php

when I call that command, the daemon runs infinitely because the file dojobs.php has the following code

while(true)
{
   code here
}

I have the following questions:

  • How do I use Monit (on a CENTOS linux 5.5) server to monitor this daemon and restart it if it has failed?
  • I have the following code inside the daemon:

    exec('nohup sendMail.php > /dev/null 2>&1 & echo $!';, $op);

(how do I make sure the nohup command above works correctly and that the sendMail.php file actually does its job? The sendMail.php is not a daemon. It sends an email and then quits.)

Thank you so much.

Fogbow answered 8/12, 2011 at 17:15 Comment(0)
O
2

Not familiar with Monit, so can't help you with that. But instead of calling exec() you can use the Process Control (pcntl_*()) extension to fork separate processes and wait for them to return a status code to the parent process, in order for it to know if the job has been successfuly completed or not.

Olpe answered 8/12, 2011 at 17:34 Comment(2)
the problem is my daemon is doing a lot of things. So it can't be waiting for processes.Fogbow
I don't mean wait like blocking the rest of the process - it's an endless loop, so you can periodically poll for return statuses.Olpe

© 2022 - 2024 — McMap. All rights reserved.