太阳当空照-Windows服务化方式Winsw应用(下)

简介: 太阳当空照-Windows服务化方式Winsw应用(下)

相关指令

参考链接:https://github.com/winsw/winsw/tree/master

install to install the service to Windows Service Controller. This command requires some preliminary steps described in the Installation guide

安装windows服务控制器,命令的细节步骤可查看,安装细节引导:https://github.com/winsw/winsw/blob/master/doc/installation.md

uninstall to uninstall the service. The opposite operation of above

卸载服务,与安装指令用途相反

start to start the service. The service must have already been installed

在服务已经安装的前提下,启动服务

stop to stop the service

停止服务

stopwait to stop the service and wait until it’s actually stopped

等待停止,直到服务结束运行,实现服务停止

restart to restart the service. If the service is not currently running, this command acts like start

重启服务,如果服务未运行,效果与启动服务相似

status to check the current status of the service

This command prints one line to the console.

  • NonExistent indicates the service is not currently installed
  • Started to indicate the service is currently running
  • Stopped to indicate that the service is installed but not currently running

查看当前服务状态

执行以上命令需要以管理员身份进行执行

案例实践

控制台程序

此处基于.Net Core3创建了一个控制台,主要内容如下,启动后,循环每隔一秒写入时间到特定文件中,

static void Main(string[] args)
{
    while (true)
    {
        string datestr = DateTime.Now.ToString("HH:mm:ss");
        Thread.Sleep(1000);
        LogMsg(datestr);
    }
}
private static void LogMsg(string msg)
{
    //System.AppDomain.CurrentDomain.BaseDirectory
    //获取程序集目录:C:\Windows\Temp\.net\sctest
    //System.Environment.CurrentDirectory
    //获取该进程从中启动的目录:C:\Windows\system32
    //System.IO.Directory.GetCurrentDirectory()
    //获取当前应用工作路径:C:\Windows\system32
    //获取当前进程所属的执行exe文件名称
    string filename = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    string dictoryPath = Path.GetDirectoryName(filename);
    string path = System.IO.Path.Combine(dictoryPath, $"{DateTime.Now.ToString("yyyy-MM-dd")}.log");
    using (var writer = File.AppendText(path))
    {
        writer.WriteLine($"当前时间:{msg}");
    }
}

配置服务

手动创建目录logs用于存储日志文件,配置文件可以参考官方案例

[sample-allOptions.xml]https://github.com/winsw/winsw/releases/download/v2.11.0/sample-allOptions.xml

[sample-minimal.xml]https://github.com/winsw/winsw/releases/download/v2.11.0/sample-minimal.xml

创建配置文件名称为test.xml,内容如下:

<service>
  <id>winswtest</id>
    <name>winsw测试服务</name>
    <description>当前用于测试winsw的服务化操作</description>
    <startmode>Automatic</startmode>
    <executable>winswtest.exe</executable>
    <startarguments></startarguments>
    <workingdirectory>%BASE%</workingdirectory>
    <logpath>%BASE%\logs</logpath>
    <log mode="roll-by-size-time">
      <sizeThreshold>10240</sizeThreshold>
    <pattern>yyyyMMdd</pattern>
      <autoRollAtTime>00:00:00</autoRollAtTime>
    </log>
    <serviceaccount>
      <username>LocalSystem</username>
    </serviceaccount>
</service>

文件目录结构如下,当前目录为E:\Study\Servers\winswtest,读者按实际需求进行更改

直接运行winswtest.exe时,能够在当前目录下生成对应的.log文本,内容为时间读秒

注册服务

注册依据模式的不同而方式不同

捆绑模式[通用]

将下载的Winsw-x64.exe放到与winswtest.exe同目录下,并重命名为test.exe,并添加配置文件test.xml,保证服务程序和配置文件同名,以管理权限运行cmd,切换目录到test.exe所在目录下,执行服务安装指令

>cd /d E:\Study\Servers\winswtest
>test.exe install
2021-08-01 22:27:22,831 INFO  - Installing service 'winsw测试服务 (winswtest)'...
2021-08-01 22:27:22,859 INFO  - Service 'winsw测试服务 (winswtest)' was installed successfully.
>>test.exe status
Stopped

表示当前服务注册成功,同时也可以直接查看对应的系统服务列表,可以发现和上述注册安装服务时,基本信息一致,需要注意的是服务在【任务管理器】-【服务】中并不会显示出来,需要去到服务列表中进行查看

全局模式[3.x]-目前不可用

将下载的Winsw-x64.exe放到一个认为合适的文件目录下,并重命名为winsw.exe,配置环境变量WINSW_HOME,路径为存放winsw.exe所在目录,此处为D:\Program Files\Winsw

添加环境变量WINSW_HOME

新建后,将环境变量添加到Pathpath中,便于在系统任意位置能够访问到winsw.exe,便于指令的执行

确认无误后,点击各级确定

打开cmd命令提示符窗口,输入winsw,查看是否收到指令提示,由于winsw本身无参数时无法任何结果反馈,此处随意输入一个参数,出现如下异常,则表示,全局环境配置成功

>winsw -h
2021-08-01 12:43:29,318 FATAL - Unhandled exception
System.IO.FileNotFoundException: Unable to locate winsw.[xml|yml] file within executable directory
   at WinSW.Program.LoadConfigAndInitLoggers(Boolean inConsoleMode)
   at WinSW.Program.Run(String[] argsArray, IServiceConfig config)
   at WinSW.Program.Main(String[] args)

出现'winsw' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

常见原因有两个:

一个是环境变量配置错误,配置路径不正确,只是配置了WINSW_HOME,并没有将环境变量添加到path/Path中,实现全局可访问

一个是当前执行的cmd命令提示窗口,在配置环境变量前就已经打开,所以造成当前窗口,环境配置无效

启动服务

捆绑模式[通用]

test.exe所在目录中,直接服务启动指令

>test.exe start
2021-08-01 23:37:07,146 INFO  - Starting service 'winsw测试服务 (winswtest)'...
2021-08-01 23:37:07,626 INFO  - Service 'winsw测试服务 (winswtest)' started successfully.
>test.exe status
Started

服务启动成功后,当前目录下,生成输出文件

全局模式[3.x]

目前官方稳定最新版本为2.x版本,所有目前无法正常使用

停止服务

捆绑模式[通用]

执行指令stop

>test.exe stop
2021-08-01 23:42:08,123 INFO  - Stopping service 'winsw测试服务 (winswtest)'...
2021-08-01 23:42:08,131 INFO  - Service 'winsw测试服务 (winswtest)' stopped successfully.
>test.exe status
Stopped

全局模式[3.x]

目前官方稳定最新版本为2.x版本,所有目前无法正常使用

卸载服务

捆绑模式[通用]

执行指令uninstall

>test.exe uninstall
2021-08-01 23:43:49,221 INFO  - Uninstalling service 'winsw测试服务 (winswtest)'...
2021-08-01 23:43:49,227 INFO  - Service 'winsw测试服务 (winswtest)' was uninstalled successfully

全局模式[3.x]

目前官方稳定最新版本为2.x版本,所有目前无法正常使用

总结

对于winsw进行了依据官方文档,进行了细化的常规操作说明,实现一个简单的应用程序,作为服务化的的服务子进程的基本操作,比较遗憾的是,全局模式并未实现成功,通过查阅,发现目前版本为2.x版本,不包含3.x的全局模式,官方最新版本master分支当前为WinSW v2.11.0,后续如果官方更新到3.x笔者将再调整文档中的内容,如果还想关系其他服务化的操作方式,可留言评论,关注笔者或者私信


相关文章
|
7天前
|
Windows
Windows——如何提取Microsoft Store的应用
Windows——如何提取Microsoft Store的应用
12 0
|
7天前
|
JavaScript Windows
NodeJs——如何获取Windows电脑指定应用进程信息
NodeJs——如何获取Windows电脑指定应用进程信息
13 0
|
1月前
|
Windows
【Windows】 Win10下报错:该文件没有与之关联的应用来执行该操作。请安装应用,若已经安装应用,请在“默认应用设置”页面中创建关联
【Windows】 Win10下报错:该文件没有与之关联的应用来执行该操作。请安装应用,若已经安装应用,请在“默认应用设置”页面中创建关联
294 1
|
2月前
|
Ubuntu Linux C语言
【opencv】opencv在windows和linux的应用
【opencv】opencv在windows和linux的应用
|
3月前
|
开发框架 .NET API
在Windows Server 2008 R2上运行.Net 8应用
在Windows Server 2008 R2上成功运行.Net 8程序,需安装三个补丁:Windows Server 2008 R2 SP1 (KB976932)是基础更新;VC_redist.x64提供MSVC库支持;KB3063858解决.NET运行时加载`kernel.dll`的路径问题。KB3063858可能需要KB2533623。详细信息和下载链接在文中给出。
235 4
|
3月前
|
C# Windows
一款.NET开源、简洁易用的Windows桌面小说阅读应用
一款.NET开源、简洁易用的Windows桌面小说阅读应用
|
3月前
|
Serverless API 数据安全/隐私保护
Serverless 应用引擎产品使用之阿里函数计算中在本地搭建Windows开发环境与阿里云函数计算进行交互如何解决
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
Windows 开发者
Windows 8.1 应用再出发 - 几种新增控件(1)
原文:Windows 8.1 应用再出发 - 几种新增控件(1) Windows 8.1 新增的一些控件,分别是:AppBar、CommandBar、DatePicker、TimePicker、Flyout、MenuFlyout、SettingsFlyout、Hub 和 Hyperlink。
1127 0
|
Windows Go 网络架构
Windows 8.1 应用再出发 - 几种新增控件(2)
原文:Windows 8.1 应用再出发 - 几种新增控件(2) 本篇我们接着来介绍Windows 8.1 的新增控件,分别是:Flyout、MenuFlyout、SettingsFlyout、Hub 和 Hyperlink。
1041 0
|
Go Android开发 Windows
Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2)
原文:Windows 8.1 应用再出发 (WinJS) - 几种新增控件(2) 上篇我们介绍了Windows 8.1 和 WinJS 中新增控件中的 AppBarCommand、BackButton、Hub、ItemContainer,本篇我们接着来介绍 NavBar、Repeater 和 WebView。
1009 0