Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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/...
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?
---
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");
<?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();
?>
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.