On Mon, Apr 10, 2000 at 02:49:17PM +0500, Areg wrote:
> Hi
> Is ther any way to make a simple program daemon?
> I'm writing a server, listening on some port & I think how to make it
> running without my help. I've tried using '&' after command line, but
> server is not responding...
> 
>   
You'll need a chunk of code that does something like:

#include <unistd.h>/*dup,setsid,fork,chdir*/
#include <stdlib.h>/*exit*/
#include <stdio.h> /*perror */

switch (fork()){
        case 0:/*parent*/
                return 0;
        case -1:/*error*/
                return 1;
}
chdir("/");
setsid();
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (dup(dup(open("/dev/null", O_APPEND)))==-1){
        perror("dup");
        return 1;
}

If you're only working on Linux and BSD's, daemon will let you
be lazy. It does the same thing as the afformentioned code, except
they use dup2 instead of dup:

#include <unistd.h>
if (daemon( 0, 0)==-1){
        perror("daemon");
        return 1;
}

have fun,

greg
-- 
military intelligence. portable C.

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [EMAIL PROTECTED]
Please read the FAQ at http://www.linux-learn.org/faqs

Reply via email to