Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-22-2003, 04:36 PM   #1
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, 04:58 PM   #2
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:

im assuming c/c++ though
Old 07-23-2003, 12:30 AM   #3
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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, 10:59 AM   #4
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.

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

Rep: Reputation: 120Reputation: 120
There is also "Advanced programming in the UNIX environment" also by Stevens. Great book
Old 07-23-2003, 04:25 PM   #6
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, 04:58 PM   #7
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:

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

have fun!


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
start service - mail daemon Nishtya Linux - Newbie 0 04-24-2004 05:12 PM
adding a new daemon (service) vjenks Linux - Newbie 3 02-11-2004 08:31 PM
Service Monitoring Daemon (MON) PokerFace Programming 1 12-04-2002 12:31 PM
I want to start Snort as a service/daemon Olusegun Linux - Software 3 10-15-2002 11:35 AM
launching a programe in startup as service /daemon prpsarathy435 Linux - Software 6 07-07-2002 11:43 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:44 PM.

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