Enable daemon to run with service daemon start,etc
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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).
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.
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?
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();
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.