daemon problem please help

This is a discussion on daemon problem please help within the C Programming forums, part of the General Programming Boards category; hi.. this is my first post here.. btw im making a daemon for processes... is there a way that i ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    somewhere out there
    Posts
    7

    daemon problem please help

    hi.. this is my first post here.. btw im making a daemon for processes...
    is there a way that i can define if the process is null or not or there is another way to detect if it is up or not...
    heres a sample of the code

    Code:
    for(;;)
    {
    fp = popen("cat /proc/*/status | grep 'httpd'", "r");
    
    if(fp == NULL)
    {
    system("/etc/init.d/httpd start");
    }else{
    fpup = popen("cat /proc/loadavg", "r");
    while(fscanf(fp, "%d ", &load) != EOF)
    {
    if(load > 4)
    {
    system("/etc/init.d/httpd restart");
    sleep(180);
    }
    }
    pclose(fpup);
    }
    pclose(fp);
    return 0;
    }
    exit(EXIT_FAILURE);
    when i run the daemon ans httpd is already up.. it starts httpd.. can someone help me? thanks..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This sounds like something that is MUCH easier to make in a shell-script, rather than C. But you could, I suppose, read the output from system("ps ax|grep httpd");

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    popen() doesn't return NULL if the program it runs produces no output. You'll still get a valid pipe, it's just that reading from it will give you EOF (or possibly something else; I imagine POSIX defines this behavior but caveat emptor).

    Also,
    Code:
    fpup = popen("cat /proc/loadavg", "r");
    seems like a rather bizarre way of writing
    Code:
    fpup = fopen("/proc/loadavg", "r");
    but perhaps you have a reason for it.

    In addition, I imagine that your
    Code:
    while(fscanf(fp, "%d ", &load) != EOF)
    loop will, quite possibly, never terminate. If fscanf() can't convert anything, it'll return 0, and leave the non-readable data in the stream, meaning the next time around, the same thing will happen. If you do use fscanf(), it's generally a good idea to check the return value against the number of conversions you expect.

    It's non-standard, but since it looks like you're on a Linux system and doing non-standard stuff anyway, I'd recommend getloadavg() if you want to peek at system load. It's a bit nicer than poking around in /proc yourself.

    As for a general response to your question, your HTTP server likely creates a pidfile that you can check; to see if the process is alive, you can use kill().

    However, if you're using Apache, it comes with an apachectl script that does the dirty work. If you run "apachectl start" and Apache's already running, it gracefully exits; otherwise Apache starts. Your distribution's init scripts quite possibly mimic this behavior, but you'd have to check that yourself.

    For the case of potential runaway processes, you really can't do much more than go through all of /proc and see if anything there is interesting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21