0

I´ve never written a daemon before and I am trying to write a rather complex one that has to connect to a MYSQL database to see if it is time (or past time) to perform a ftp retrieval, then try to retrieve some ftp files if the time is right. Then it needs to import the ftp data into the MYSQL database, and finally update several associated MYSQL tables. Then wait & loop back, of course.

I generally write in php. C syntax is no problem for me in terms of straight coding, but it has been so long that I have no idea what tools and libraries would be preferred to handle such tasks.

What I am looking for is some examples of working daemons that do more than write to a log file. Specifically, I like to see how somebody has gotten a working daemon to retrieve an ftp file and query and/or update a MYSQL database. I´m curious about how people handle errors and what kind of objects they use to do handle database calls and file transfers.

I am using the 10.04 Ubuntu server.

Thank you for your consideration.

  • Seems like a stackoverflow thing to me. But do see http://pear.php.net/package/System_Daemon, and http://code.google.com/p/concentre-daemon/. – Zoredache Sep 03 '10 at 05:05
  • @Zoredache i second that and it should be perhaps moved. – Prix Sep 03 '10 at 06:06

2 Answers2

0

has to connect to a MYSQL database to see if it is time (or past time) to perform a ftp retrieval

Why not using a simple cronjob for this task ? this way you can still make it in php, it would not be running full time but instead schedule on the time you want by the cronjob and would probably save you resources.

From php.net manual:

<?php

$pid = '/tmp/my_code_pid';
if (file_exist("$pid")) { # to prevent your code from running 
    echo "Proccess already running.";# when another instance is already running
    exit;
}

touch($pid); # create the pid file

// define some variables
$local_file = 'local.zip';
$server_file = 'server.zip';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

// close the connection
ftp_close($conn_id);

// Do your mysql things

unlink("$pid") if (file_exist("$pid")); # erase the pid file once the runtime is complete
?>

Now to make your cronjob rule to run every 5 minutes for example:

*/5 * * * * /usr/bin/php /home/my_user/my_code.php 1> /dev/null

NOTE: Use whereis php to make sure of your php path

To set the above schedule do:

crontab -e

Once it enters the editor (assuming your default editor is VI), type:

:$ and press ENTER this will move you to the last line of the file

o will get you in text mode for the next line which will be a new line

esc will make you exit the text mode so you can save your file

:wq and press ENTER will save and exit the editor

:q! and press ENTER if u made any mistake which will exit without saving the changes.

Prix
  • 4,881
  • 3
  • 24
  • 25
  • you're right. I missed your "simple cronjob" disclaimer. Comment deleted. – EEAA Sep 03 '10 at 02:12
  • Downside of a cronjob: If it's scheduled to run faster than it completes, the processes can pile up and possibly eventually kill the machine. – pjz Sep 03 '10 at 02:53
  • @pjz true but you can simple go around it with a simple proccess verification on your php file or even by creating a file when being executed and exit if that file is found. – Prix Sep 03 '10 at 03:17
0

A daemon is generally defined as a long-running task that runs in the 'background', not under direct control of a user. To a programmer that basically means:

  1. it's got a loop that probably has some kind of delay or wait on it so it will end up being 'long-running'. So putting a while (!file_exist("/tmp/mydaemon_killswitch")) { before the code and sleep($timeperiod); } after the code will, in some sense, make it a daemon.

  2. it does a 'fork off and die' to disassociate from the tty. Note that another way to simulate this from the commandline is to login, do 'nohup $yourprog & ', and then log out. $yourprog won't die, but the controllling tty will go away and it will keep running in the 'background'.

pjz
  • 10,595
  • 1
  • 32
  • 40