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.
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.
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]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.