LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-22-2003, 03:36 PM   #1
wlmapp3
LQ Newbie
 
Registered: Jul 2003
Location: Upper Marlboro, Maryland
Distribution: None
Posts: 2

Rep: Reputation: 0
Angry How To Program Daemon(Service) Application


Hello Everyone and thanks in advance,

I am very frustrated. I am a newcomer to the Linux software development world, and I am trying to find out how to develop an application that will run as a service or daemon.

I can google hundreds of examples and etc. on how to do this for Windows but not Linux. Now before someone bends me over and states that this request is non-sensical and not necessary for Linux because you can write a shell script to accomplish the same task, please explain to me how my running application receives various events from the OS and knows when to cleanup open resources, communicate with other running apps, etc. when Linux is shutting down.

I understand how this mechanism works in Windows, and that there are API's, callbacks, etc. that I can use to receive start, stop, pause, barf events from the OS when something occurs.

Executing a kill command or running a kill script on shutdown does not seem graceful to me and some enlightenment on how Linux runs services or etc. Would be great.

What do I need to do?

Start my application on startup.
Yes it listens for TCP requests and does something with them.
Yes it communicates with other processes and servers.
It needs to know when the OS is going down, notify peer systems, close files, lower the toilet seat and flush.
When the system boots back up, everything is peachy.

Again, sorry for asking this seamingly FAQ based question, if you do not have time for a detailed explanation, please send me a URL, recommendation for a book, or something else.

Thank you for your assistance.

William Lee Mapp, III
 
Old 07-22-2003, 03:58 PM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
this ought to help: http://www.gnu.org/manual/glibc-2.2....-Handling.html

im assuming c/c++ though
 
Old 07-22-2003, 11:30 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
You have a pretty complex list of requirements. [list=1][*]Startup the application from the Linux startup scripts. This will differ with different flavors of Linux. For Slackware you would add your program to the rc scripts in the /etc/rc.d directory. The program will need to daemonize itself after it starts. This is programmatically done with the daemon call.[*]Listen for TCP requests. Here you will need to create a socket and bind to a port. man 3 socket[*]Communicates with other processes. Not sure how you want to do this... You could talk with sockets, message queues, shared memory segments or even signals.[*]Know when the kernel is going down and cleanup. The kernel sends out a signal to all processes when it is about to shutdown. It's your job to catch the signal an do whatever cleanup needs to be done. Be sure to do only what is necessary since the kernel will send a sig kill moments after the shutdown signal.[*]System boots back up. See step one[/list=1]

hope this helps
 
Old 07-23-2003, 09:59 AM   #4
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
If you want to do this in C then best resource would be Richard Stevens book

UNIX Network Programming, Volume 1, Second Edition: Networking APIs: Sockets and XTI, Prentice Hall, 1998, ISBN 0-13-490012-X.

http://www.kohala.com/start/unpv12e.html

This book covers signal handling and has a complete chapter on how to daemonize a process.
 
Old 07-23-2003, 02:08 PM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
There is also "Advanced programming in the UNIX environment" also by Stevens. Great book
 
Old 07-23-2003, 03:25 PM   #6
wlmapp3
LQ Newbie
 
Registered: Jul 2003
Location: Upper Marlboro, Maryland
Distribution: None
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks everyone for your assistance. I really appreciate it.
 
Old 07-23-2003, 03:58 PM   #7
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
its pretty fun and you can write all sorts of servers:

http://www.ecst.csuchico.edu/~beej/guide/net/

this is by far the best socket programming guide ive seen online.


have fun!
 
  


Reply



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
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
Service Monitoring Daemon (MON) PokerFace Programming 1 12-04-2002 11:31 AM
I want to start Snort as a service/daemon Olusegun Linux - Software 3 10-15-2002 10:35 AM
launching a programe in startup as service /daemon prpsarathy435 Linux - Software 6 07-07-2002 10:43 PM

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

All times are GMT -5. The time now is 06:38 PM.

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