您的位置:首页 > 运维架构 > Nginx

使用Windows Service Wrapper快速创建一个Windows Service 如nginx

2016-09-05 08:49 399 查看
前言今天介绍一个小工具的使用。我们都知道Windows Service是一种特殊的应用程序,它的好处是可以一直在后台运行,相对来说,比较适合一些需要一直运行同时不需要过多用户干预的应用程序,这一类我们称之为“服务”吧

编写Windows Service其实是不难的,尤其是如果有Visual Studio的话。但是仍然是有不少童鞋觉得略显繁琐,同时,如果有一些其他的程序,我们只拿到一个exe,或者一个bat,但又想让他象服务一样运行,怎么办呢

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

wsw的基本使用一般的使用步骤是:

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

2.编写一个同名的xml文件 如:myapp.xml

3.使用Install命令进行安装

myapp.exe install

[/code]
4.如果想要卸载,则使用Uninstall命令

myapp.exe uninstall

[/code]
5. 重启

myapp.exe restart

[/code]6:停止
myapp.exe stop

[/code]7: 启动:
myapp.exe start

[/code]



https://github.com/kohsuke/winsw



配置文件的格式请参考:https://github.com/kohsuke/winsw

我的一个例子如下

<service>

<id>nginx</id>

<name>nginx</name>

<description>nginx</description>

<executable>c:\nginx\nginx.exe</executable>

<logpath>c:\nginx\</logpath>

<logmode>roll</logmode>

<depend></depend>

<startargument>-c</startargument>

<startargument>c:\nginx\conf\nginx.conf</startargument>

<startargument>-c</startargument>

<startargument>c:\nginx</startargument>

<stopexecutable>c:\nginx\nginx.exe</stopexecutable>

<stopargument>-p</stopargument>

<stopargument>c:\nginx</stopargument>

<stopargument>-s</stopargument>

<stopargument>stop</stopargument>

</service>

[/code]运行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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: