|
Home > Archive > Unix Programming > June 2004 > How to "deamonize" ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to "deamonize" ?
|
|
| Franki 2004-06-26, 10:11 am |
| Hello,
(running config : slackware with a 2.4.26 kernel )
I have a http server which run normally (a fork which accept connections) .
But I would like
to change the behaviour of my server (I would like to daemonize it ) :
- When I start it from the shell : #my_http_serv I would like to get
the same behaviour than apache.
example : #my_http_serv start
# <- here i come back
to the original shell.
How Can I do that please ?
Thanks.
p.s :I have written all the information ( pid , ...) into files ....\
--
Franki
| |
| Rich Gibbs 2004-06-26, 10:11 am |
| Franki said the following, on 06/23/04 13:09:
> Hello,
>
> (running config : slackware with a 2.4.26 kernel )
> I have a http server which run normally (a fork which accept connections) .
> But I would like
> to change the behaviour of my server (I would like to daemonize it ) :
> - When I start it from the shell : #my_http_serv I would like to get
> the same behaviour than apache.
> example : #my_http_serv start
> # <- here i come back
> to the original shell.
> How Can I do that please ?
>
> Thanks.
>
> p.s :I have written all the information ( pid , ...) into files ....\
>
I'm not entirely sure that I understand your question, but if you are
just asking how to invoke your server so it runs in the background, all
you need is:
# my_http_serv start &
and you'll get your command prompt back.
Standard daemons are usually started in an initialization script. (On
my Debian Linux system, these are in the '/etc/rc<n>.d' directories --
<n> is a digit for the run level.)
If you are asking how to _write_ a daemon, then I suggest you have a
look at W. Richard Stevens's _Advanced programming in the UNIX Environment_.
--
Rich Gibbs
rgibbs@alumni.princeton.edu
"If you find yourself in a hole, stop digging." (Will Rogers)
| |
| Martin Blume 2004-06-26, 10:11 am |
| "Franki" schrieb
>
> (running config : slackware with a 2.4.26 kernel )
> I have a http server which run normally (a fork which accept
> connections) . But I would like
> to change the behaviour of my server (I would like to
> daemonize it ) :
man daemon
HTH
Martin
| |
| Fabien R 2004-06-26, 10:11 am |
| > - When I start it from the shell : #my_http_serv I would like to get
> the same behaviour than apache.
> example : #my_http_serv start
> # <- here i come back
> to the original shell.
> How Can I do that please ?
>
> Thanks.
>
> p.s :I have written all the information ( pid , ...) into files ....\
This could be a good start.
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#ifdef USE_PROTOTYPES
int makeDaemon(int argc, char **argv,int (*parent)())
#else
int makeDaemon(argc, argv, parent)
int argc;
char **argv;
int (*parent)();
#endif
{
int ii,noctty,noblock,fd[3];
long parent_pid, child_pid;
parent_pid = (long)getpid();
if (0L > (child_pid=(long)fork())) /* failed */
return(-1);
else if (child_pid) /* parent */
{
if (parent) (void) parent(parent_pid,argc,argv);
exit(0);
}
/* child */
for(ii=0;3 > ii;ii++) /* detach terminal */
{
if (isatty(ii))
{
fd[ii] = -1;
close(ii);
}
else
fd[ii] = ii;
}
setsid(); /* become session leader */
if (0 > (child_pid=(long)fork())) /* failed */
return(-1);
else if (child_pid) /* parent */
exit(0);
umask(000); /* allow all users access to files created */
noctty = noblock = 0; /* reset file mode mask */
#ifdef O_NOCTTY
noctty |= O_NOCTTY;
#endif
#ifdef O_NDELAY
noblock |= O_NDELAY;
#endif
#ifdef O_NONBLOCK
noblock |= O_NONBLOCK;
#endif
/* open handles to nul device */
for(ii=0;3 > ii;ii++) /* detach terminal */
{
if (0 <= ii) continue;
switch(ii)
{
case 0:
open("/dev/null",O_RDONLY|noctty); /* stdin */
break;
case 1:
open("/dev/null",O_APPEND|noctty); /* stdout */
break;
case 2:
open("/dev/null",O_APPEND|noblock|noctty); /* stderr */
break;
}
}
return(0); /* daemon should be installed */
} /* int makeDaemon(int argc, char **argv,int (*parent)()) */
| |
| Pete Brett 2004-06-26, 10:11 am |
| "Martin Blume" <mblume@socha.net> wrote:
>"Franki" schrieb
>
>man daemon
daemon() is not always available... and in any case, it gives less control.
If you just want to run in the background, you can fork() and have the parent
exit. The example code shown in Stevens does various other things like closing
stdin, stdout and stderror, chdir to "/" and ensuring that the daemon process is
not associated with a process group which could receive a HUP signal on modem
hangup etc.
Pete
| |
| Bjorn Reese 2004-06-26, 10:11 am |
| On Wed, 23 Jun 2004 18:09:24 +0100, Franki wrote:
> I have a http server which run normally (a fork which accept connections) .
> But I would like
> to change the behaviour of my server (I would like to daemonize it ) :
See section 1.7 in the FAQ of this newsgroup:
http://www.erlenstar.demon.co.uk/unix/
--
mail1dotstofanetdotdk
| |
| Franki 2004-06-26, 3:49 pm |
|
"Bjorn Reese" <breese@see.signature> wrote:
>
> See section 1.7 in the FAQ of this newsgroup:
>
> http://www.erlenstar.demon.co.uk/unix/
>
Ok thanks , for this usefull FAQ ;)
And thanks to all person who have answer my question.
--
Franki
|
|
|
|
|