如何利用mono把.net windows service程序迁移到linux上

How to migrate a .NET Windows Service application to Linux using mono?

 

写在最前:之所以用要把windows程序迁移到Linux上,主要是由于一些成本问题,这个就不多解释了。

如何把之前用.net写的windows服务程序迁移到linux上运行。答案是有很多种的,今天我只提一下mono(我只实验了mono,呵呵)。

如何在Linux部署mono,并成功的运行.net程序,还请大家多多查询吧,我在这方面也只是搭建成功了,遇到的问题不足够多,就不误导大家了。

用mono来在Linux运行一个.net程序还是很容易的,但如何运行一个后台服务程序呢,今天也是费了九牛二虎之力,最后借鉴一下stackoverflow中的一个回答来诠释这个难题吧。

Under Linux, deamons are simple background processes. No special control methods (e.g start()stop()) are used as in Windows. Build your service as a simple (console) application, and run it in the background. Use a tool like daemonize to run a program as a Unix daemon, and remember to specify mono as the program to be activated.

As noted by others, mono-service is a host to run services built with the ServiceProcess assembly. Services built for Windows can use this method to run unmodified under Linux. You can control the service by sending signals to the process (see man page).

我英文不是太好,只能大概理解其中的意思。里边的滋味大家可以自己细细品味。

大致意思就是说,第一种方式:在Linux下可以把windows服务改造成一个控制台程序来运行。第二种方式:用mono-service来运行windows的服务程序。前提是你这个程序是有.net写成的。

mono-service运行时参数又该如何配置呢?

命令格式:mono-service [options] program.exe

-d:DIRECTORYUse this option to specify the working directory for the service. The default is the current directory.

-l:LOCKFILESpecifies the file to use for locking, the default is a filename constructed in /tmp based on the name of the program that hosts the service.

-m:MESSAGEName to show in the syslog.

-n:NAMEUse this to specify the service to be launched (if the program contains more than one service). The default is to run the first defined service.

--debug:Use this option to prevent mono-service from redirecting stdin and stdout and prevent the program to be sent to the background. Equivalent to

--no-daemon:Use this option to prevent mono-service from redirecting stdin and stdout and prevent the program to be sent to the background. Equivalent to --debug.

例:mono-service -l:/var/run/Myservice-lock.pid MyService.exe  (这个-l参数一定要加上

控制服务(这几种操作的区别请参考windows的使用方式,这里我就不做过多解释了):

暂停: kill -USR1 `cat <lock file>`

继续: kill -USR2 `cat <lock file>`

停止: kill `cat <lock file>`

 这里的不是单引号,是数字1左边的那个点号。(够坑)

 

注意:Use mono-service to run services that use the 1.0 assemblies and use mono-service2 to run services that use the 2.0 assemblies.

mono-service可以运行.net 1.0的程序集,mono-service2可以运行.net 2.0的程序集。

对上面删除的内容做一下纠正,之前用的2.0的程序做的测试,没有发现这个问题。

后来翻了一些资料发现:mono-service已经不再支持.net程序了,mono-service2支持的还是.net2.0的程序。特别说明mono-service支持的是4.5的程序了,至于它对4.0支持如何,自己实验一下吧,我就不一一实验了。下面我为这个说法提供一些佐证:

 

 

 

在这里还有一种方式:http://www.cnblogs.com/sjhrun2001/archive/2009/03/10/1408264.html

 

posted @ 2014-11-05 19:05  Eric.Z  阅读(3405)  评论(1编辑  收藏  举报