.NET跨平台實踐:用C#開發Linux守護進程

來源:互聯網
上載者:User

標籤:ati   open   sum   pac   import   mars   代碼   分享   dll   

Linux守護進程(Daemon)是Linux的後台服務進程,它脫離了與控制終端的關聯,直接由Linux init進程管理其生命週期,即使你關閉了控制台,daemon也能在後台正常工作。

一句話,為Linux開發與控制台無關的,需要在後台長時間不間斷啟動並執行“服務程式”,Daemon技術是非常重要的。

Daemon程式一般用c/c++開發。不過,我今天要講的,不是怎麼用c/c++開發daemon,而是用C#!

一,建立Daemon程式:

用VS建立一個控制台項目,假設名稱是MyDaemon,輸入下邊的代碼:

using System.Runtime.InteropServices;using System.Threading;namespace MyDaemon{    class Program    {        static void Main(string[] args)        {            int pid = fork();            if (pid != 0) exit(0);            //設定“交談群組長”,與父進程脫離            setsid();            pid = fork();            if (pid != 0) exit(0);            //已經進程“守護進程”工作狀態了!            //關閉所有開啟的檔案描述符            int max = open("/dev/null", 0);            for (var i = 0; i <= max; i++) { close(i); }            //重設檔案掩模            umask(0);            //執行你的程式過程            DaemonMain(args);        }        /// <summary>        /// Daemon工作狀態的主方法        /// </summary>        /// <param name="aargs"></param>        static void DaemonMain(string[] aargs)        {            //你的工作代碼...            //daemon時,控制台輸入輸出資料流已經關閉            //請不要再用Console.Write/Read等方法
//阻止daemon進程退出 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([MarshalAs(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); }}

然後編譯為 MyDaemon.exe。

二,部署和運行:

.net 程式在linux運行,一般都會使用mono這個.net架構,不過,為了簡單方便,我這裡使用 AnyExec來運行這個程式(關於AnyExec,請參閱:不裝mono,你的.NET程式照樣可以在Linux上運行!)。

1,把 MyDeamon.exe放到anyexec的app檔案夾;

2,把 "any"這個程式複製為 MyDeamon;

3,運行:見證神奇的時間到了!請你在linux控制終端上輸入: ./MyDaemon,哈哈,怎麼沒有反應? 其實,不是沒有反應,是你這個 MyDaemon程式已經在後台跑起來了!

輸入 “ps -ef”,看看!

看到那個 MyDaemon了吧!這次啟動並執行PID是11618,父進程是的PID是1,1是誰?linux init!

4,退出daemon程式:daemon程式不會與控制台輸入輸出進行互動,所以,用Console.ReadLine之類的方法控制進程的退出是不現實的。那麼,怎麼關閉這個在後台啟動並執行 daemon呢? 最簡辦法就是用ps -ef查出這個進程的PID號,然後用kill命令終止它。比如當前啟動並執行這個 mydaemon的PID號是 11618,你只需要輸入 kill -9 11618,就能終止它的運行。

.NET跨平台實踐:用C#開發Linux守護進程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.