. NET cross-platform practice: use C # To develop the Linux daemon,
The Linux Daemon is a background service process in Linux. It is separated from the control terminal and managed by the Linux init process. Even if you close the console, daemon can also work properly in the background.
In a word, the Daemon technology is very important to develop a "service program" that has nothing to do with the console for Linux.
Daemon programs are generally developed in c/c ++. However, what I want to talk about today is not how to use c/c ++ to develop daemon, but how to use C #!
1. Create a Daemon program:
Use VS to create a console project. Assume the project name is MyDaemon. Enter the following code:
Using System. Runtime. InteropServices; using System. Threading; namespace MyDaemon {class Program {static void Main (string [] args) {int pid = fork (); if (pid! = 0) exit (0); // set the "session leader", which is out of the setsid (); pid = fork (); if (pid! = 0) exit (0); // The "daemon" process is in the working state! // Close all open file descriptors int max = open ("/dev/null", 0); for (var I = 0; I <= max; I ++) {close (I) ;}// reset the file mask umask (0); // execute your program process DaemonMain (args );} /// <summary> /// main method of the Daemon working state /// </summary> /// <param name = "mongogs"> </param> static void DaemonMain (string [] aargs) {// your work code... // when daemon is enabled, the Console input/output stream is disabled. // do not use the Console again. write/Read
// Stop the daemon process from exiting while (true) {Thread. sleep (1000) ;}} [DllImport ("libc", SetLastError = true)] static extern int fork (); [DllImport ("libc", SetLastError = true)] static extern int setsid (); [DllImport ("libc", SetLastError = true)] static extern int umask (int mask); [DllImport ("libc", SetLastError = true)] static extern int open ([financialas (UnmanagedType. LPStr)] string pathname, int flags); [DllImport ("libc", SetLastError = true)] static extern int close (int fd); [DllImport ("libc ", setLastError = true)] static extern int exit (int code );}}
Then compile it as MyDaemon.exe.
Ii. deployment and operation:
.. Net programs run in linux. Generally, mono is used. net framework. However, for ease of use, I use AnyExec to run this program (for more information about AnyExec, see: do not install mono, your.. NET program can still run on Linux !).
1. Put MyDeamon.exe in the app folder of anyexec;
2. Copy the "any" program as MyDeamon;
3. Run: It's time to witness the magic! Please input:./MyDaemon on the linux control terminal. Haha, why didn't you respond? In fact, it's not no response. It's because your MyDaemon program has been running in the background!
Enter "ps-ef" and check it out!
See the MyDaemon! The running PID is 11618. Who is the PID of the parent process? Linux init!
4. Exit the daemon program: the daemon program does not interact with the Console input and output. Therefore, it is unrealistic to control the process exit using methods such as Console. ReadLine. So how can we disable the daemon running in the background? The simplest way is to use ps-ef to find the PID of the process, and then use the kill command to terminate it. For example, if the current mydaemon PID Number is 11618, you only need to enter kill-9 11618 to terminate the operation.
(This article is original from yun.com. After investigation, no similar technical articles have been found on the Internet. You are welcome to repost it)