Re: daemon without pcntl_fork

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Lars Torben Wilson wrote:
2009/8/17 Jim Lucas <lists@xxxxxxxxx>:
Does anybody know how to use PHP as a daemon without the use of pcntl_fork.

http://php.net/pcntl_fork

Hi Jim,

AFAIK you can't. Read on. . .

I don't want to have to have a person have a special/custom compilation
of PHP just to run a simple daemon.

My system:  OpenBSD 4.5 w/PHP v5.2.8

I want to launch a daemon out of the /etc/rc.local when the system starts.

My goal is to write a script that will be launched from /etc/rc.local
when a system boots.  I want it to be detached from any shell or ssh
login that I launch it from also.

Anybody have any idea on how to do this?

I have played with system() and it does work.

What you've done below is not create a daemon, but a background
process. It's still attached to the shell you started it in (try
killing the shell you started it from and see what happens). There are
other differences too. IMHO the approach you've used here does have
its uses, and I've used it (and still do) when it's appropriate, but
when what you need is a daemon, then faking it with a background
process just isn't enough.

Compiling in pcntl isn't really that big of a deal--depending on
exactly what you're trying to accomplish. Why is it a problem in your
case? Perhaps there is another way around the issue which has a
cleaner solution. For the cases I've run into, pcntl has worked
admirably.


I want this to be a system that works out of the box.  For the most part.

I am expecting to have phone system vendors and low-level IT personal trying to install this thing.

I don't want to have to field tons of questions on "How do I compile this p&#*(_fork thing into PHP?

Anyways, could I compile a customer build of the php cli and include that into my package?

Would things be statically links to the binary. ie every thing it needed would be built into it. Or would I have to rip out as much as possible to make sure that it was compatible with a persons system?

Thanks for the input!

test.php:
<?php
echo 'Starting';
system('/usr/local/bin/php test_cli.php >/dev/null &');
echo 'Done';
?>

test_cli.php
<?php

for( $i=1; $i<=10; $i++ ) {
       echo "Echo {$i}\n";
       sleep(1);
}

echo 'Done';

?>

The above, when called, launches test_cli.php and detaches it from the
cli and returns to the system prompt


Well, after writing all this out, I think I have answered by own question.

If anybody else has a better suggestion, I am all ears.

If you have a better way of doing it, please share.

Also, a second piece to this would be a script to manage
(start/stop/restart/etc...) the parent daemon.

Something along the line of apachectl or similar.

TIA!

Update to the last email also.

I found another device that does RS232 to ethernet:

http://www.hw-group.com/products/portstore2/index_en.html

Anybody work with one of these?

Not me. But I've solved similar problems using ser2net (see
http://sourceforge.net/projects/ser2net/ ), sometimes running it on a
small embedded Linux device. Works great and I don't have to pay
someone else to sell me a free solution. :) But again, it depends on
your actual situation and what problem you're trying to solve. On the
face of it the device you linked looks OK. (I'm afraid I missed your
earlier post on the topic.)

Again, thanks!

Jim Lucas

I'm not trying to shoot down any ideas you've had or anything, just
wondering what's so bad about compiling pcntl in and hoping that maybe
you can save a few bucks on the serial-to-network problem by making
use of existing free software. Post more about what your situation is
and who knows? Maybe a fakey-daemon using background processes and a
proprietary serial-to-network device really is the best answer for
you.

Either way, good luck!


Regards,

Torben



--
Jim Lucas

   "Some men are born to greatness, some achieve greatness,
       and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
    by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux