. NET cross-platform practice: Developing Linux daemons in C #

Source: Internet
Author: User

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 {Staticvoid Main (String[] args) {int PID =Fork ();if (pid! =0) Exit (0);//Set the session leader to detach from the parent processSetsid (); PID =Fork ();if (pid! =0) Exit (0);//The process "Daemon" is working!//Close all open file descriptorsint max = open ("/dev/null",0);for (var i =0; I <= Max; i++{Close (i);}//Reset File Mask umask (0);//Execute your program processDaemonmain (args); }///<summary>///Main method of daemon working state///</summary>///<param name= "Aargs" ></param>Staticvoid Daemonmain (String[] Aargs) {//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 exitingwhile (True) {Thread.Sleep (1000); }} [DllImport ("libc", SetLastError =True)]StaticexternIntFork (); [DllImport ("libc", SetLastError =True)]StaticexternIntSetsid (); [DllImport ("libc", SetLastError =True)]Staticexternint Umask (Intmask); [DllImport ("libc", SetLastError =True)]Staticexternint open ([MarshalAs (UNMANAGEDTYPE.LPSTR)]String pathname,int flags); [DllImport ( "libc" SetLastError = true)] extern int Close ( int FD); [DllImport ( "libc" SetLastError = true)] extern int exit ( code); }}

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! The PID of this run is 11618, the parent process is the PID 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 11618, you only need to enter kill-9 11618, you can terminate its operation.

(This article turns from: http://www.cnblogs.com/yunei/p/5352944.html)

. NET cross-platform practice: Developing Linux Daemons in C # (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.