You are not logged in.
Pages: 1
hello, I would like to write a daemon which monitors on mysql table and process all rows that he didn't already process, how would I do that ?
I'm kind of experienced in cpp, but only on competitions, so I don't know how could I connect to mysql ...
If there's easier way in some other programming language, then please tell me which and how, tnx.
Offline
Hello, mfolnovic!
To solve your problem in C++ you need to read the topics of reference manual related to C/C++ API (which both can be used in your case): http://dev.mysql.com/doc/refman/5.1/en/ … -apis.html
I think there should be books describing those APIs in more obvious way than the official documentation. Or you can try to find some tutorials.
You'll find the excellent explanation how to make daemons using POSIX API in the following book: http://apuebook.com/. See the source code coming with book, to find the examples.
Take a glance at what Qt can do. There can be special classes for making daemons and for interaction with databases (but I'm not sure about that). Also if you want your program to have GUI Qt can be the best choice for you.
Here is the documentation to the last release of Qt: http://doc.trolltech.com/4.4/index.html
But I beleive that C/C++ aren't the best languages to use in your case Your problem is where Python and Ruby give a taste of one's quality.
Offline
Here's a python daemon script: http://hathawaymix.org/Software/Sketches/daemon.py
It drops privileges, logs, etc., a good starting point for writing your own daemon.
MySQL in Python is pretty easy, there should be a lot of resources available.
Offline
If there's easier way in some other programming language, then please tell me which and how, tnx.
Uhm... yes! php, perl, python, ruby, ... you could even do it in sh
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
You can make a daemon-like script in PHP which is very easy to use, and I can help you if you want - I've never really gotten into databases though so it'd be a learning experience for both of us
-dav7
Last edited by dav7 (2008-10-05 14:07:42)
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
yes, finaly, I decided to make it in php, and made it, and it works
but I don't know how to make it daemon, so it runs every 15 s ...
Last edited by mfolnovic (2008-10-05 14:56:54)
Offline
Ah, you want pcntl_fork, which is a lot easier to use than it might look.
You simply say $pid = pcntl_fork(), then check $pid. If it's -1, the fork failed for some reason (so you probably want to die() or something to end the whole script), if the $pid is not -1 and not 0 you're the parent process and you want to die() because that's how you become a daemon (you create a child process, then kill the parent process, and the child process continues to execute but detached from the terminal that created the parent process), and if the PID is 0, we are the child process, and we want to put all our code here.
Code example modified from php.net:
<?php
$pid = pcntl_fork();
if ($pid == -1) {
die("fork() call asploded!\n");
} else if ($pid) {
// we are the parent
print "fork()ed successfully.\n");
die();
}
// We'd only leave the if/then block if we're the child
// Put your existing code here
?>
I didn't test the above code however, so see how it goes. Beware that since it should work as a daemon, you're going to want to check your system for stray copies of php lurking around if/as you test it since it'll be forking itself and remaining resident like a daemon should!
-dav7
Last edited by dav7 (2008-10-05 16:08:03)
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
Ah, you want pcntl_fork, which is a lot easier to use than it might look.
You simply say $pid = pcntl_fork(), then check $pid. If it's -1, the fork failed for some reason (so you probably want to die() or something to end the whole script), if the $pid is not -1 and not 0 you're the parent process and you want to die() because that's how you become a daemon (you create a child process, then kill the parent process, and the child process continues to execute but detached from the terminal that created the parent process), and if the PID is 0, we are the child process, and we want to put all our code here.
Code example modified from php.net:
<?php $pid = pcntl_fork(); if ($pid == -1) { die("fork() call asploded!\n"); } else if ($pid) { // we are the parent print "fork()ed successfully.\n"); die(); } // We'd only leave the if/then block if we're the child // Put your existing code here ?>
I didn't test the above code however, so see how it goes. Beware that since it should work as a daemon, you're going to want to check your system for stray copies of php lurking around if/as you test it since it'll be forking itself and remaining resident like a daemon should!
-dav7
so I just paste that code, put my code down, run it, and it should work as daemon?
Offline
It should. It might not, but it definately should.
-dav7
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
how can I check is it running as daemon?
Offline
ok, done it, tnx everyone
Offline
Awesome
Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.
Offline
There are some more things you should/probably want to do in php ( set_time_limit (0);, declare (ticks=1);, binding signals with pcntl_signal(), posix_setsid (); in the child, write a pid file etc)
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
Pages: 1