How can I automatically start a daemon at boot time.

Hi masters,

           I am still learning trades in kernel. I am trying to learn the basic of daemon programming. Can any one tell me how can I start a daemon automatically during boot up. I will be greatfull if anyone post some example code to the above task. 
         Also what are the other things that you must know when you try daemon programming? Can any one help me?

a basic unix daemon does the following:

fork
close all filedescriptors (stdout,stderr, etc)
chdir /
signal handeling (sighup, sigterm etc)
while
do stuff
sleep(xx)
done

(example in C: daemon.c)

Red Hat example on how to install startup scripts:

to start a deamon at system startup in redhat you need a init script.
it should be placed in /etc/init.d

example of init script :

# chkconfig: 3 99 1
# description: my daemon

case "$1" in
'start')
/usr/local/bin/mydaemon
;;

'stop')
pkill mydaemon
;;

'restart')
pkill -HUP mydaemon
;;

esac

the first line will tell chkconfig to start the daemon in run level 3 with priority 99 and kill it as priority 1 when server shutdowns.

to install the startup script use the following: chkconfig --add ./scriptabove
Now it will start when the server boots.

to start it right away use: service <name_of_initfile> start

Hope this helps somewhat!

Thanks s93366 ...... That was very useful..............That cleared all.........

Hi s93366,

Thanks so much for the excellent tutorial. I works great on Linux environment. However, When I was trying it on Solaris environment, it doesn't work since there's no chkconfig on Linux. Do you have any idea on how to make it work on Solaris? I am new to Linux/Unix, and is learning Linux/Unix. Any help will be greatly appreciated.