当前位置: 首页 > news >正文

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

前言

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

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

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

wsw的基本使用

一般的使用步骤是:

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

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

3.使用Install命令进行安装

4.如果想要卸载,则使用Uninstall命令

 

 

https://kenai.com/projects/winsw/pages/Home

This project creates a wrapper executable that can be used to host any executable as an Windows service.

Download

The binaries are available here for download.

Usage

During your development...

  1. Take winsw.exe from the distribution, and rename it to your taste (such as myapp.exe)
  2. Write myapp.xml (see Configuration Syntax for more details)
  3. Place those two files side by side when you deploy your application, because that's how winsw.exe discovers its configuration.

At runtime...

  • To install a service, run myapp.exe install
  • To start a service, run myapp.exe start
  • To stop a service, run myapp.exe stop
  • To restart a service, run myapp.exe restart
  • To uninstall a service, run myapp.exe uninstall

When there's a problem, these commands also report an error message to stderr. On a successful completion, these commands do no produce any output and exit with 0.

In addition, you can also run myapp.exe status to have it print out the current status of the service to stdout. Again, any error encountered during the processing would cause output to be reported to stderr.

All these commands use the same set of exit code to indicate its result.

Deferred File Operations

To support self updating services, winsw offers a mechanism to perform file operations before a service start up. This is often necessary because Windows prevents files from overwritten while it's in use.

To perform file operations, write a text file (in the UTF-8 encoding) at myapp.copies (that is, it's in the same directory asmyapp.xml but with a different file extension), and for each operation add one line:

  • To move a file, write "src>dst". If the 'dst' file already exists it will be overwritten.

The success or failure of these operations will be recorded in the event log.

Contributions Welcome

If you are interested in joining the project, let me know. I'm always interested in more committers.

 

 

配置文件的格式请参考:https://kenai.com/projects/winsw/pages/ConfigurationSyntax 

我的一个例子如下

image

运行install后,在services.msc中可以看到这个服务

image

image

 

 

相关知识

在Windows系统里面,还有一类特殊的服务,他们都是用一个特殊的程序启动的(svchost),如下

image

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

 

在Linux系统上面,也有一个类似的工具(而且更加强大),supervisor,有兴趣的童鞋可以参考

http://supervisord.org/introduction.html

转载于:https://www.cnblogs.com/chenxizhang/p/4688051.html

相关文章:

  • [IE9] 解决了傲游、搜狗浏览器在IE9下网页截图的问题
  • [.net 面向对象程序设计进阶] (19) 异步(Asynchronous) 使用异步创建快速响应和可伸缩性的应用程序...
  • [转]activiti5用户任务分配
  • 数据结构:链表 链表按结点中第j个数据属性排序(冒泡排序法)
  • 批处理命令调用WINRAR对文件进行压缩
  • 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations
  • 批处理命令拷贝文件
  • 我4年前写的第一个ruby程序
  • c# 调用c DLL 所传参数不正确
  • 离职那天我们复员——Leo网上答疑53
  • Spark工作机制-调度与任务分配
  • DT大数据梦工厂 第74讲
  • TCP SYN-Cookie背后的人和事
  • Unity3D NGUI 点击穿透问题的解决方案
  • C++ VS C#(4):枚举,结构体
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • CSS 提示工具(Tooltip)
  • Git的一些常用操作
  • Git同步原始仓库到Fork仓库中
  • happypack两次报错的问题
  • JSONP原理
  • Laravel Mix运行时关于es2015报错解决方案
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • Linux中的硬链接与软链接
  • ng6--错误信息小结(持续更新)
  • React-生命周期杂记
  • SpringBoot几种定时任务的实现方式
  • swift基础之_对象 实例方法 对象方法。
  • Vue.js 移动端适配之 vw 解决方案
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 回顾2016
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 每天一个设计模式之命令模式
  • 前端性能优化--懒加载和预加载
  • 微信小程序填坑清单
  • 微信支付JSAPI,实测!终极方案
  • 新手搭建网站的主要流程
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • 移动端高清、多屏适配方案
  • ​LeetCode解法汇总307. 区域和检索 - 数组可修改
  • ###C语言程序设计-----C语言学习(3)#
  • #DBA杂记1
  • #设计模式#4.6 Flyweight(享元) 对象结构型模式
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (附源码)ssm高校实验室 毕业设计 800008
  • (十三)Flask之特殊装饰器详解
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (转) Android中ViewStub组件使用
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .net Application的目录
  • .NET Core SkiaSharp 替代 System.Drawing.Common 的一些用法
  • .NET6 命令行启动及发布单个Exe文件