Results 1 to 4 of 4
Hi everyone,
I'm building a daemon in C with fork() in Ubuntu7.
What it needs to do is:
- Read a file with dates.
- Get first date not downloaded ...
- 02-13-2008 #1
open url in a daemon
Hi everyone,
I'm building a daemon in C with fork() in Ubuntu7.
What it needs to do is:
- Read a file with dates.
- Get first date not downloaded and open a web pages with GET parameters.
- Read the page content for a link to a TXT file.
- Download the TXT file from the link found above.
I could build the daemon and every 5 seconds writes a syslog (just to see it's working).
What I need is:
- How do I do the Web Page call, and the TXT download ?
- Another thing is to do the start/stop.
Is there any way to call a process method o something to tell the fork child created (which stays in a loop), to stop wroking and end? or the only way is to kill the process?
I have this done in Java, but I need it as a background service.
Thanks to all,
- 02-13-2008 #2There are two ways that I know of to do this.How do I do the Web Page call, and the TXT download ?
The first way is to use system(), and run either wget or lynx -source or lynx -dump.
An alternative to this first way is, instead of using system(), use a combination of fork() (not the main fork() which you're already doing), one of the exec*() calls, and waitpid().
The second way is to take a deep breath and do the UNIX network programming yourself. I know of no really good, comprehensive web resource for this; I'd recommend using the Addison Wesley book UNIX Network Programming, volume 1 by W. Richard Stevens of happy memory.
Well, killing the process would do the job. But it would be more graceful to have that process finish at the top of the loop, wouldn't it? If that's the case, send the process a signal from your main process. In the child process, have a signal handler which simply sets a global flag and does nothing else. In your main loop in the child process, check that flag at the top of the loop.Is there any way to call a process method o something to tell the fork child created (which stays in a loop), to stop wroking and end? or the only way is to kill the process?
Handling signals is simple enough that you should be able to google for resources on it. Come back here if you have questions.--
Bill
Old age and treachery will overcome youth and skill.
- 02-14-2008 #3
Great, thanks a lot!
All this worked... but he web part, I did it with socket programming.
I wrote the signal part, for the SIG_TERM and tested from console (kill -s TERM pid#) and in the code I wrote a syslog when that signal is received, and worked !!!
Now, I have 3 more questions for which I looked around and not found...
1- Signal: The only way to know the child's pid is to save it in a file ? (like 'program.pid'); or there is another way to know it?
This is because I want to add the start/stop inside the main program, so when I call the stop the main prog. get the child pid and send the signal.
Another way I thought about, is to do the start / stop in a shell script; in which I do a ps -aux and search for the program pid# and send the signal with the (kill -s TERM pid#)
2- Socket: How do I now if there is more data available to be received.
The problem is, that if I do a receive and there's no data, then it blocks there.
3- Syslog: Is there any way to send the log to a user custom facility.
When I installed the spamassassin I configured it to send syslog to a facility I added to syslog.conf.
I want to do the same from my code.. Is there any way? or I have to open / write and close the log file ?
thanks a lot...
- 02-14-2008 #4The child can know its own pid by using getpid(). The parent can know its child's pid by examining the result of fork().The only way to know the child's pid is to save it in a file ?
Open the socket asynchronously and use select(). Also, I think it's more customary for ordinary web situations like this to use write() and read(), rather than send() and recv().How do I now if there is more data available to be received.
The problem is, that if I do a receive and there's no data, then it blocks there.
Get the Stevens book. It will pay off in the long run.
Do this at the command line:Is there any way to send the log to a user custom facility.
You do not want:Code:man 3 syslog
Code:man 2 syslog # this is wrong
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote