LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 09-03-2009, 12:48 PM   #1
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Rep: Reputation: 16
Restarting a server daemon


Hello all,

I'm writing a program which acts like a server daemon. I want to know if it's possible to make the program restart itself when I receive a particular type of message from a client that I am listening to.

Thanks

William
 
Old 09-03-2009, 01:23 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
I suggest making your daemon respond appropriately to SIGHUP. How you (or the client) generate the signal is up to you and there are myriad methods.
 
Old 09-03-2009, 01:28 PM   #3
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Original Poster
Rep: Reputation: 16
What do I do in the function that I call from catching SIGHUP? I want to restart the main block after properly closing anything that has been opened by my server.
 
Old 09-03-2009, 03:50 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Being very general, I'd say clean up in the child process and have it exit, then fork again. One way to do that is to set up a specific return value in the child and watch for that in the parent. If the child exits with that value, fork another one.
 
Old 09-04-2009, 12:39 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,093

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
If you want a clean full restart, see exec http://linux.die.net/man/3/exec
 
Old 09-04-2009, 12:53 PM   #6
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Original Poster
Rep: Reputation: 16
wow that's a powerful system call. Thanks
 
Old 09-04-2009, 04:12 PM   #7
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Original Poster
Rep: Reputation: 16
However, I believe that forking is still more graceful, since I can know that the stack rolled back up when it returns, whereas when you do an exec you won't know how much junk you accumulate on the stack.
 
Old 09-04-2009, 09:22 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You might just run a script like this as your daemon:
Code:
#!/bin/bash


function daemon_loop()
{
  false #<-- replace with daemon stuff (perhaps exec daemon $*)
}


while ( daemon_loop $* ); do
  sleep 1
done
This will restart the daemon when it exits unless it exits with an error. You'd need to make the actual daemon exit without error when given e.g. the SIGHUP signal to indicate that it should be restarted.
Kevin Barry

PS Another option is to design it to work with xinetd, which won't actually start it until a connection is made; there will be one fork per connection.

Last edited by ta0kira; 09-04-2009 at 09:37 PM.
 
  


Reply

Tags
daemon, programming


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between restarting a daemon and a service gsiva Linux - Server 6 07-21-2009 05:49 AM
Restarting the inetd daemon hypercondor Linux - Newbie 3 05-06-2008 03:09 PM
restarting x-server Zuggy Linux - Newbie 2 08-04-2006 03:16 AM
Restarting X Server unwrittenLaww Linux - Newbie 11 04-20-2004 01:34 PM
Restarting the X Server jworld900 Linux - General 12 01-28-2004 11:21 AM


All times are GMT -5. The time now is 02:39 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration