LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Tags used in this thread
Popular LQ Tags ,

Reply
 
Thread Tools
Old 09-03-2009, 01:48 PM   #1
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1
Restarting a server daemon


[Log in to get rid of this advertisement]
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
windows_xp_2003 wtruong is offline  
Tag This Post ,
Reply With Quote
Old 09-03-2009, 02:23 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 238
Blog Entries: 2
Thanked: 11
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.
windows_xp_2003 raconteur is offline     Reply With Quote
Old 09-03-2009, 02:28 PM   #3
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1

Original Poster
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.
windows_xp_2003 wtruong is offline     Reply With Quote
Old 09-03-2009, 04:50 PM   #4
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 238
Blog Entries: 2
Thanked: 11
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.
windows_xp_2003 raconteur is offline     Reply With Quote
Old 09-04-2009, 01:39 AM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,429
Thanked: 325
If you want a clean full restart, see exec http://linux.die.net/man/3/exec
windows_xp_2003 chrism01 is offline     Reply With Quote
Old 09-04-2009, 01:53 PM   #6
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1

Original Poster
wow that's a powerful system call. Thanks
windows_xp_2003 wtruong is offline     Reply With Quote
Old 09-04-2009, 05:12 PM   #7
wtruong
LQ Newbie
 
Registered: May 2009
Distribution: ubuntu
Posts: 18
Thanked: 1

Original Poster
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.
windows_xp_2003 wtruong is offline     Reply With Quote
Old 09-04-2009, 10:22 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware 11.0, Slackware 12.1, slamd64 12.2, Ubuntu Server 8.04, Kubuntu 9.04
Posts: 2,081
Thanked: 39
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 10:37 PM..
linux ta0kira is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
difference between restarting a daemon and a service gsiva Linux - Server 6 07-21-2009 06:49 AM
Restarting the inetd daemon hypercondor Linux - Newbie 3 05-06-2008 04:09 PM
restarting x-server Zuggy Linux - Newbie 2 08-04-2006 04:16 AM
Restarting X Server unwrittenLaww Linux - Newbie 11 04-20-2004 02:34 PM
Restarting the X Server jworld900 Linux - General 12 01-28-2004 12:21 PM


All times are GMT -5. The time now is 08:08 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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration