使用Windows Service Wrapper快速建立一個Windows Service 如nginx

來源:互聯網
上載者:User

標籤:

前言

今天介紹一個小工具的使用。我們都知道Windows Service是一種特殊的應用程式,它的好處是可以一直在後台運行,相對來說,比較適合一些需要一直運行同時不需要過多使用者幹預的應用程式,這一類我們稱之為“服務”吧

編寫Windows Service其實是不難的,尤其是如果有Visual Studio的話。但是仍然是有不少童鞋覺得略顯繁瑣,同時,如果有一些其他的程式,我們只拿到一個exe,或者一個bat,但又想讓他象服務一樣運行,怎麼辦呢

答案就是可以使用如下的一個工具,它的名稱就叫:Windows Service Wrapper,簡稱WSW。


wsw的基本使用

一般的使用步驟是:

1. 下載wsw的最新版本,放在任意的位置,修改成你想要的任何名字如“myapp.exe”

2.編寫一個同名的xml檔案 如:myapp.xml

3.使用Install命令進行安裝 

 
  1.  myapp.exe install

4.如果想要卸載,則使用Uninstall命令

 
  1. myapp.exe uninstall

5. 重啟 

 

 
  1. myapp.exe restart
6:停止
 
  1. myapp.exe stop
7: 啟動:
 
  1. myapp.exe start

 

https://github.com/kohsuke/winsw

 設定檔的格式請參考:https://github.com/kohsuke/winsw

我的一個例子如下

 
  1. <service>
  2. <id>nginx</id>
  3. <name>nginx</name>
  4. <description>nginx</description>
  5. <executable>c:\nginx\nginx.exe</executable>
  6. <logpath>c:\nginx\</logpath>
  7. <logmode>roll</logmode>
  8. <depend></depend>
  9. <startargument>-c</startargument>
  10. <startargument>c:\nginx\conf\nginx.conf</startargument>
  11. <startargument>-c</startargument>
  12. <startargument>c:\nginx</startargument>
  13. <stopexecutable>c:\nginx\nginx.exe</stopexecutable>
  14. <stopargument>-p</stopargument>
  15. <stopargument>c:\nginx</stopargument>
  16. <stopargument>-s</stopargument>
  17. <stopargument>stop</stopargument>
  18. </service>

運行install後,在services.msc中可以看到這個服務

  

 

 

相關知識

在Windows系統裡面,還有一類特殊的服務,他們都是用一個特殊的程式啟動的(svchost),如下

那麼,這又是怎麼一回事情呢?有興趣的童鞋可以參考  http://www.howtogeek.com/howto/windows-vista/what-is-svchostexe-and-why-is-it-running/

 

在Linux系統上面,也有一個類似的工具(而且更加強大),supervisor,有興趣的童鞋可以參考

http://supervisord.org/introduction.html 



來自為知筆記(Wiz)

使用Windows Service Wrapper快速建立一個Windows Service 如nginx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.