Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined! guillermo.odone's Avatar
    Join Date
    Dec 2005
    Location
    Argentina
    Posts
    7

    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,

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    How do I do the Web Page call, and the TXT download ?
    There are two ways that I know of to do this.

    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.

    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?
    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.

    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.

  3. #3
    Just Joined! guillermo.odone's Avatar
    Join Date
    Dec 2005
    Location
    Argentina
    Posts
    7
    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...

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    The only way to know the child's pid is to save it in a file ?
    The child can know its own pid by using getpid(). The parent can know its child's pid by examining the result of fork().
    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.
    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().

    Get the Stevens book. It will pay off in the long run.
    Is there any way to send the log to a user custom facility.
    Do this at the command line:
    Code:
    man 3 syslog
    You do not want:
    Code:
    man 2 syslog    # this is wrong
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...