LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   System_Daemon HowTo (https://www.linuxquestions.org/questions/linux-newbie-8/system_daemon-howto-784013/)

demiron 01-22-2010 07:59 AM

System_Daemon HowTo
 
Hi,

I need a background app. which is processing every seconds.
It should call a php file includes update mysql inserts etc.

i've tried cronjobs but it works every min. it's not good for me.
i've search about Daemons and i finally found Pear::System__Daemons extension.i installed it, but i can't start it.

I download package and upload like /home/user_xx/public_html/daemons/...

Can you help me about this?

Thanks.

TB0ne 01-22-2010 08:46 AM

Quote:

Originally Posted by demiron (Post 3836443)
Hi,

I need a background app. which is processing every seconds.
It should call a php file includes update mysql inserts etc.

i've tried cronjobs but it works every min. it's not good for me.
i've search about Daemons and i finally found Pear::System__Daemons extension.i installed it, but i can't start it.

I download package and upload like /home/user_xx/public_html/daemons/...

Can you help me about this?

Thanks.

Help you about what?? Very vague problem, and no real information to work with. You say "i can't start it"...how about giving us what error(s) you're getting, on what version/distro of Linux? What's your program written in? How often does it need to run?

If it's written in PHP, why don't you just put a sleep statement in there, and loop back through after a short pause?

demiron 01-22-2010 08:56 AM

Thanks for reply,

You can check here: http://pear.php.net/manual/tr/packag...les.simple.php

---
when i try to exucute app file like ->
~ chmod a+x ./Daemon.php
~ ./Daemon.php

error:
root@server [/home/user_xxx/public_html/daemons]# ./Daemon.php
: No such file or directory
: command not found2:
./Daemon.php: line 3: //: is a directory
./Daemon.php: line 4: syntax error near unexpected token `('
'/Daemon.php: line 4: ` $value = ini_get("error_reporting");

---

i just do instructions of Pear page -> http://pear.php.net/manual/en/packag...tem-daemon.php


it should works every seconds. (always)
i need this because when i try solve this problem with php loops it takes %70+ memmory.

Server software is latest CentOs 32Bit.
Packages installed.

demiron 01-22-2010 08:58 AM

this is "Daemon.php" -> exucute
Code:

<?php
        require_once('System/Daemon.php');
       

        class Daemon
        {
                public $script;
                public $name;
                public $delay;
                public $listeners;
               
                public function Daemon($script=false, $delay=1)
                {
                        if ($script === false)
                                $script = $_SERVER["SCRIPT_NAME"];
                       
                        $this->script = $script;
                        $this->name = strtolower(basename($this->script, ".php"));
                        $this->delay = $delay;
                       
                        $this->listeners = array();
                        $this->init();
                }
               
                public function start()
                {
                        System_Daemon::start();  // Spawn Deamon!
                       
                        while(!System_Daemon::isDying())
                        {
                                $this->logInfo("Robo Daemon still running");

                                foreach ($this->listeners as $callback)
                                        call_user_func_array($callback, array());
                                       
                            System_Daemon::iterate($this->delay);
                        }
                }
               
                public function logInfo($message)
                {
                        System_Daemon::log(System_Daemon::LOG_INFO, $message);
                }
               
                public function log($message)
                {
                        $this->logInfo($message);
                }
               
                public function run($callback)
                {
                        $this->listeners[] = $callback;
                }
               
                public function init()
                {
                        System_Daemon::setOption("appName", $this->name);
                        System_Daemon::setOption("appDescription", $this->name);
                        System_Daemon::setOption("appDir", getcwd());
                        System_Daemon::setOption("authorName", "Demiron");
                        System_Daemon::setOption("authorEmail", "asitoprak@gmail.com");

                        System_Daemon::writeAutoRun();
                }
               
                public function pid()
                {
                        return System_Daemon::getOption("appPidLocation");
                }
               
                public function stop()
                {
                        System_Daemon::stop();
                }
        }
       
        class ScriptDaemon extends Daemon
        {
                public function ScriptDaemon($script)
                {
                        parent::__construct($script);
                        $this->run(array($this, "loop"));
                }
               
                public function loop()
                {
                        $output = shell_exec("php $this->script");
                        $this->log($output);
                }
        }

?>



this is other file -> "Daemon"

Code:

#!/usr/bin/php -q
<?php
       
        require_once('Daemon.php');
       
        ## USE DAEMON IN HERE ##
       
        if (count($argv) != 3)
                exit("Usage: daemon [script] [start|stop|restart]");
       
        $method = $argv[2];
        $script = $argv[1];
       
        if (!file_exists($script))
                exit("Could not find script: $script");
               
        $daemon = new ScriptDaemon($script);
       
        switch ($method)
        {
                case "start":
                        system("php cron.php");
                        $daemon->start();
                        break;
                       
                case "stop":
                        kill_daemon($daemon);
                        break;
                       
                case "restart":
                        kill_daemon($daemon);
                        sleep(0.5);
                        $daemon->start();
                        break;
                       
                default:
                        exit("Could not understand method: $method");
        }
       
        function kill_daemon($daemon) {
                $pid = file_get_contents($daemon->pid());
                posix_kill($pid, SIGKILL);
        }
       
        echo "[ OK ]\n";
        exit();
?>


TB0ne 01-22-2010 01:29 PM

Quote:

Originally Posted by demiron (Post 3836515)
this is "Daemon.php" -> exucute

Well, you need to run PHP scripts with "php <script name>", not just calling the name. Also, putting the "#!/usr/bin/php" is a mashup of shell script and PHP, which won't work.

If you're trying to call this from the command-line, there are much better languages than PHP. Perl or bash would be best for such things, in my opinion...PHP is normally used for web-applications. And if your program is using 70+% of resources just by looping...you've got problems in your code. Sleeping for a second or two shouldn't be a problem, unless your processes aren't terminating within that window. If they're not, put a check in to see if it's done, before sleeping.


All times are GMT -5. The time now is 01:29 PM.