Is it wise to use PHP for a daemon?

前端 未结 17 1411
庸人自扰
庸人自扰 2020-11-28 09:18

I wish to create a background process and I have been told these are usually written in C or something of that sort. I have recently found out PHP can be used to create a da

相关标签:
17条回答
  • 2020-11-28 10:06

    Go for it. I had to do it once also. Like others said, it's not ideal but it'll get-er-done. Using Windows, right? Good.

    If you only need it to run occasionally (Once per hour, etc). Make a new shortcut to your firefox, place it somewhere relevant. Open up the properties for the shortcut, change "Target" to:

    "C:\Program Files\Mozilla Firefox\firefox.exe" http://localhost/path/to/script.php
    

    Go to Control Panel>Scheduled Tasks Point your new scheduled task at the shortcut.

    If you need it to run constantly or pseudo-constantly, you'll need to spice the script up a bit.

    Start your script with

    set_time_limit(0);
    ob_implicit_flush(true);
    

    If the script uses a loop (like while) you have to clear the buffer:

    $i=0;
    while($i<sizeof($my_array)){
         //do stuff
         flush();           
         ob_clean();
         sleep(17);
         $i++;
    }
    
    0 讨论(0)
  • 2020-11-28 10:09

    If you know what you are doing sure. You need to understand your operating system well. PHP generally isn't suited for most daemons because it isn't threaded and doesn't have a decent event based system for all tasks. However if it suits your needs then no problem. Modern PHP (5.3+) is really stable and doesn't have any memory leaks. As long as you enable the GC and don't implement your own memory leaks, etc you'll be fine.

    Here are the stats for one daemon I am running: uptime 17 days (last restart due to PHP upgrade). bytes written: 200GB connections: hundreds connections handled, hundreds of thousands items/requests processed: millions

    node.js is generally better suited although has some minor annoyances. Some attempts to improve PHP in the same areas have been made but they aren't really that great.

    0 讨论(0)
  • 2020-11-28 10:10

    A cron job and a little bit of bash scripting should be everything you need by the sounds of it. You can do things like:

    $file=`mysqlquery -h server < "select file from table;"`
    ffmpeg $file -fps 50 output.a etc.
    

    so bash would be easier to write, port and maintain IMHO than to use PHP.

    0 讨论(0)
  • 2020-11-28 10:13

    A cron-job would probably work just fine, if near-instant actions is not required.

    I'm just about to put live, a system I've built, based on the queueing daemon 'beanstalkd'. I send various small messages from (in this case, PHP) webpage calls to the daemon, and a PHP script then picks them up from the queue and performs various tasks, such as resizing images or checking databases (often passing info back via a Memcache-based store).

    To avoid long-running processes, I've wrapped it in a BASH script, that, depending on the value returned from the script ("exit(1);") will restart the script, for every (say) 50 tasks it's performed. If it's restarting because I plan it to, it will do so instantly, any other exit value (the default is 0, so I don't use that) would pause a few seconds before it was restarted.

    0 讨论(0)
  • 2020-11-28 10:13

    If you do decided to go down the daemon route, there is a great PEAR module called System_Daemon which I've recently used successfully on a PHP v5.3.0 installation. It is documented on the authors blog: http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php

    If you have PEAR installed, you can install this module using:

    pear install -f System_Daemon
    

    You will also need to create a initialisation script: /etc/init.d/<your_daemon_name>

    Then you can:

    • Start Daemon: /etc/init.d/projNotifMailDaemon start
    • Stop Daemon: /etc/init.d/projNotifMailDaemon stop

    Logs are kept at: /var/log/<your_daemon_name>.log

    0 讨论(0)
提交回复
热议问题