Daemons and Server Load question
I'm going to try my hand at writing a daemon to monitor a directory and if a file is dropped it detects what is new and runs a script to read that file, etc.
I'm new to Linux daemon programming but have an example of how to properly create a working daemon (just the bare bones). I also know C and C++ (this will be done in C using gcc, of course). I'll be working on the Ubuntu Server 6.06.1 version.
My question is, how much load is on a server when a daemon is running? I mean, I know mysqld will do something when someone accesses the MySQL database, for example, and it can get quite busy if you have a lot of folks doing database queries.
If I have my daemon running say, once a second, would that be about right? Should I use a timer to wait 1 second before polling the directory for a file?
I'm also unclear on another thing. In the example program (2nd example at http://www-theorie.physik.unizh.ch/~...owto/daemonize
His second example ends with:
/* Finish up */
syslog( LOG_NOTICE, "terminated");
Which is part of int main... Now does this mean that it does the program in int main... then ends? Is it that the program ends and then reloads itself? This btw, will be started from init.d I presume (someone correct me if I'm wrong in where it's supposed to be started/loaded on boot).
I'm not quite sure what to do here... :scratch:
Any helpful hints and tips would be appreciated.
"If I have my daemon running say, once a second, would that be about right? Should I use a timer to wait 1 second before polling the directory for a file?"
Rather than looping on a directory read to find out if any processing needs to be done you should have your program called only when it is needed. You should have the program which creates the file pipe the information to your new program instead, or something similar.
As long as you use some level of latency in the daemon, that will be a huge relief on the system. I find that even a .01s wait in a monitor loop doesn't draw that much from the system. A few things will drain resources, though, such as intensive algorithms and the load of multiple threads. Allocation of dynamic/shared memory will draw resources, also.
If the daemon is just there to wait for an event, it shouldn't draw any more resources than calling a program to do the operation manually at those times. If it's meant to run constantly, that's more of an issue of if you can afford to have that program run even if it isn't a daemon.
On a reasonably fast system (1GHz) I'd say any wait loop latency higher than .1s will be close to unnoticeable.
Thank you! :)
Turns out I've been instructed that we'll be monitoring it using a script (I have to write) from a cron job. So I won't need to write the daemon after all.
But I still want to do it just to see how daemons work and how to write them, for my own information. So I'll be trying your code. Another thing is, that originally, there was to be a php script to write a file and thus a directory monitor notice the file and launch a script to read the file and do what the file is asking (codes to initiate different actions). Now I see my idea was bit cumbersome.
But still going to play around with it just for grins. :)
And I just in general want to learn to write daemons in case I need to in the future.
Here is a working example:
|All times are GMT -5. The time now is 11:30 PM.|