The Linux daemon (Daemon) is a Linux background service process that is disconnected from the control terminal and is managed directly by the Linux init process, and even if you close the console, Daemon works in the background.
In a word, daemon technology is very important for Linux to develop a "service program" that is independent of the console and needs to run continuously in the background for a long time.
The daemon program is generally developed in C + +. However, what I am going to talk about today is not how to develop daemon in C + +, but use c#!
One, create the daemon program:
Create a new console project with VS, assuming the name is Mydaemon, enter the code below:
usingSystem.Runtime.InteropServices;usingSystem.Threading;namespacemydaemon{classProgram {Static voidMain (string[] args) { intPID =Fork (); if(PID! =0) {exit (0); } //set the session leader to detach from the parent processSetsid (); PID=Fork (); if(PID! =0) {exit (0); } //has entered the "daemon" working state! //Close all open file descriptors intmax = open ("/dev/null",0); for(vari =0; I <= Max; i++) {close (i); } //Resetting file MasksUmask0); //Execute your program processDaemonmain (args); } /// <summary> ///Main method of daemon working state/// </summary> /// <param name= "args" ></param> Static voidDaemonmain (string[] args) { //your work code ...//console input and output stream is closed when daemon//please do not use Console.write/read and other methods//prevent daemon process from exiting while(true) {Thread.Sleep ( +); }} [DllImport ("libc", SetLastError =true)] Static extern intFork (); [DllImport ("libc", SetLastError =true)] Static extern intSetsid (); [DllImport ("libc", SetLastError =true)] Static extern intUmaskintmask); [DllImport ("libc", SetLastError =true)] Static extern intOpen ([MarshalAs (UNMANAGEDTYPE.LPSTR)]stringPathnameintflags); [DllImport ("libc", SetLastError =true)] Static extern intCloseintFD); [DllImport ("libc", SetLastError =true)] Static extern intExitintcode); }}
Then compile to MyDaemon.exe.
Second, deploy and run:
. NET programs run on Linux and generally use the. NET Framework of Mono, however, for simplicity, I use Anyexec to run this program (for Anyexec, see: Don't install Mono, your. NET program can still run on Linux! )。
1, put the MyDeamon.exe into the Anyexec app folder;
2, the "any" this program is copied to Mydeamon;
3, run: witness the magical time is up! Please enter on the Linux control Terminal:./mydaemon, Haha, why didn't you react? In fact, is not unresponsive, is you this Mydaemon program has been running in the background!
Enter "Ps-ef" and see!
See that Mydaemon! This running PID is 27979, the parent process is the PID is 1,1 is who? Linux init!
4, Exit Daemon Program: The daemon program does not interact with console input and output, so it is unrealistic to control the exit of a process using methods such as Console.ReadLine. So, how do you close the daemon that runs in the background? The simplest way is to use PS-EF to isolate the PID number of the process and then terminate it with the KILL command. For example, the current running Mydaemon PID number is 27979, you only need to enter kill-9 27979, you can terminate its operation.
Ext.: http://www.cnblogs.com/yunei/p/5352944.html
. NET cross-platform practice: Developing the Linux daemon in C #-daemon