LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-22-2010, 07:59 AM   #1
demiron
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Rep: Reputation: 0
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.
 
Old 01-22-2010, 08:46 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,680

Rep: Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971
Quote:
Originally Posted by demiron View Post
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?
 
Old 01-22-2010, 08:56 AM   #3
demiron
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 01-22-2010, 08:58 AM   #4
demiron
LQ Newbie
 
Registered: Jan 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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();
?>
 
Old 01-22-2010, 01:29 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,680

Rep: Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971Reputation: 7971
Quote:
Originally Posted by demiron View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux Multicasting HOWTO.HOWTO Join Multicast Groups ar24458 Linux - Networking 0 04-02-2007 01:55 AM
Howto... Skorch_Creature Linux - Hardware 2 05-22-2006 03:30 PM
Howto - 2.6.10 artistikone Slackware 3 01-27-2005 05:33 PM
howto isdn??? Pimp Linux - General 0 08-03-2003 06:32 PM
Howto update KDE and howto switch off kdm/gdm Canaris Linux - Software 1 06-15-2003 08:40 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration