LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Enable daemon to run with service daemon start,etc (http://www.linuxquestions.org/questions/programming-9/enable-daemon-to-run-with-service-daemon-start-etc-601364/)

baddah 11-21-2007 03:00 AM

Enable daemon to run with service daemon start,etc
 
Hi,

I have a php script that runs as a deamon.I start it up with php deamon.php from my /etc/rc.local file.What must i do so that i can use linux commands such as chkconfig deamon and service deamon start/restart on it?

Thanks

harry edwards 11-22-2007 03:46 PM

chkconfig and service are wrappers for the 'init' scripts. To use them you need to add a script into /etc/rc.d/init.d/ If you inspect the contents of the directory you'll see all of the existing scripts.

chkconfig is used to assign each script to a particular run level. This is evident by ruining 'chkconfig --list'.

You'll need to copy and amend any of the script to suite you purpose then tell chkconfig when to run it i.e.

chkconfig --level 35 <your script>

baddah 12-02-2007 08:31 AM

Hi,

Ok,i have had a look at the scripts,and i added my script /etc/init.d.It works fine with service start.and chkconfig on.

My question now is.how would i gou about to notify my daemon when the system shuts down?I know i should do a chkconfig off,or put the script in /etc/rc6.d.but what can i do to tell the deamon(say its a simple while(1) loop),that it must stop what its doing,and shut the service down.I assume i must have some interrupt checker of some kind?Can anyone give me some info how i need to go about getting this working?

example..Say this my daemon

Code:

while (1) do
    print "Deamon is running";
end

What must i put inside this loop to realize,it should should stop(beacuse of a sutdown or service stop).

Thanks

unSpawn 12-02-2007 10:08 AM

On runlevel change init sends the process a TERM and then a KILL so (using chkconfig to) link your script in /etc/init.d to /etc/rc0.d/K<number>nameofscript and /etc/rc6.d/K<number>nameofscript should be enough.

baddah 12-02-2007 10:54 AM

HI Thanks for the reply.

I understand the whole procedure with adding it.My only problem is,how do i,in my deamon that is already running,catch this TERM signal,and send all connected to the deamon.(Its a socket server)a notification that we're going down.something like.

Code:

while (1) do
    /*do stuff */
   
    /* realize we have received TERM signal
      * notify all users */
end

how can i,in a infinite loop,catch this TERM signal and work with it?

baddah 12-02-2007 11:36 AM

Hi,

Looks like i got the answer.Thanks for the help.if anyone else is looking for an answer on this try something like(in php)

Code:

// signal handler function
function sig_handler($signo) {

        switch ($signo) {

                case SIGTERM:
                        shutmedown(); // handle shutdown tasks
                        break;
                default:
                        // handle all other signals
        }
}

/* daemon start */
while(1) {
       
        /*
          do stuff
          */

        // check for TERM signal
        pcntl_signal(SIGTERM, "sig_handler");
}

function shutmedown() {
        echo "TERM SIGNAL found from OS we are going down";
        exit();
}


unSpawn 12-02-2007 05:51 PM

Well done posting the answer yourself.


All times are GMT -5. The time now is 03:48 AM.