How to "deamonize" ?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > How to "deamonize" ?




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How to "deamonize" ?  
Franki


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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







[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Rich Gibbs


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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)






[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Martin Blume


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

"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







[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Fabien R


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

> - 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)()) */





[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Pete Brett


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

"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 paren
t
exit. The example code shown in Stevens does various other things like closi
ng
stdin, stdout and stderror, chdir to "/" and ensuring that the daemon proces
s is
not associated with a process group which could receive a HUP signal on mode
m
hangup etc.

Pete





[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Bjorn Reese


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:11 PM

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






[ Post a follow-up to this message ]



    Re: How to "deamonize" ?  
Franki


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 08: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







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:52 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register