LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2007, 03:00 AM   #1
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Rep: Reputation: 30
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
 
Old 11-22-2007, 03:46 PM   #2
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
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>
 
Old 12-02-2007, 08:31 AM   #3
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Original Poster
Rep: Reputation: 30
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
 
Old 12-02-2007, 10:08 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 12-02-2007, 10:54 AM   #5
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Original Poster
Rep: Reputation: 30
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?

Last edited by baddah; 12-02-2007 at 10:55 AM.
 
Old 12-02-2007, 11:36 AM   #6
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 188

Original Poster
Rep: Reputation: 30
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();
}
 
Old 12-02-2007, 05:51 PM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Well done posting the answer yourself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Register a daemon as service bittus Linux - General 6 05-22-2007 09:37 PM
How to make TCP server a daemon / service much like a dhcpd service? rajat Linux - Networking 1 05-22-2007 01:29 AM
start service - mail daemon Nishtya Linux - Newbie 0 04-24-2004 04:12 PM
adding a new daemon (service) vjenks Linux - Newbie 3 02-11-2004 07:31 PM
I want to start Snort as a service/daemon Olusegun Linux - Software 3 10-15-2002 10:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration