把.netcore console 安装到Windows 系统服务。
用个工具:NSSM 下载:最新的那个pre版本 http://www.nssm.cc/download
测试.netcore 的一个控制台程序(Console),仅仅用来定时写入一些日志, 代码如下:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System; namespace TestConsoleService
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
CreateHostBuilder(args).Build().Run();
} public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<DataService>();
});
//.UseWindowsService();
}
}
:
DataService 是一个后台服务性质的类,用来记录日志到文件,继承至BackgroundService,
代码如下:
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace TestConsoleService
{
public class DataService : BackgroundService
{
private static string _logFile =Path.Combine( Directory.GetCurrentDirectory() ,"Log.txt");
private static long i = 0;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Do some work
try
{
Timer timer = new Timer(RunWork,null,1,2000);
Console.CancelKeyPress += Console_CancelKeyPress;
}
catch (Exception ex)
{
Log("Error :" + ex.Message);
}
} private void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("are you exit?");
e.Cancel=true;
} private static void RunWork(object o) {
i++;
Log(""+DateTime.Now+ " " + i+"\r\n");
Console.WriteLine("" + DateTime.Now + " " + i); } private static void Log(string s) {
try
{
File.AppendAllText(_logFile, s);
}
catch { } } } }
然后将 nssm.exe 拷贝到项目bin目录,输入命令
nssm.exe install TestConsoleService
会弹出一个UI配置界面,console的exe路径选择填进去OK。
在任务管理器服务选项卡里救恩能够看到创建的 TestConsoleService 服务。
参考文档命令:
NSSM - the Non-Sucking Service Manager
Managing services from the command line
nssm's core functionality has always been available from the command line.
Service installation
nssm install <servicename>
nssm install <servicename> <program>
nssm install <servicename> <program> [<arguments>]
By default the service's startup directory will be set to the directory containing the program. The startup directory can be overridden after the service has been installed.
nssm set <servicename> AppDirectory <path>
Service removal
nssm remove
nssm remove <servicename>
nssm remove <servicename> confirm
Service management
As of version 2.22, nssm offers basic service management functionality. nssm will also accept a service displayname anywhere that a servicename is expected, since Windows does not allow a service to have a name or display name which conflicts with either the name or display name of another service. Both the service name (also called the service key name) and its display name uniquely identify a service.
Starting and stopping a service
nssm start <servicename>
nssm stop <servicename>
nssm restart <servicename>
Querying a service's status
nssm status <servicename>
Sending controls to services
nssm pause <servicename>
nssm continue <servicename>
nssm rotate <servicename>
另外一篇图文参考:https://www.cnblogs.com/emrys5/p/nssm-netcore.html
把.netcore console 安装到Windows 系统服务。的更多相关文章
- Windows Server 2008 R2下将nginx安装成windows系统服务
一直在Linux平台上部署web服务,但是最近的一个项目,必须要用windows,不得已再次研究了nginx在windows下的表现,因为Apache httpd在Windows下表现其实也不算太好, ...
- Windows Server 2008 R2下将JBoss安装成windows系统服务
JBoss版本是jboss-4.2.3.GA-jdk6.zip,操作系统是Windows Server 2008 R2. 1.系统已安装好java环境,JAVA_HOME已配置好: 2.下载所需文件. ...
- MongoDB安装,启动,注册为windows系统服务
MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...
- 使用srvany.exe将程序安装成windows服务的详细教程
srvany.exe介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说srva ...
- 使用srvany.exe把程序安装成windows服务
srvany.exe介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用的小工具,用于将任何EXE程序作为Windows服务运行.也就是说srva ...
- 将Tomcat加入windows系统服务
将Tomcat加入windows系统服务 将Tomcat加入服务 1.修改bin目录中的service.bat: REM 添加下面的一行 set CATALINA_HOME=%cd% 如果从来没有安装 ...
- 使用srvany.exe把程序安装成windows服务的方法
http://mazhihui.iteye.com/blog/1294431 srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的 ...
- MongoDB安装为Windows服务方法与注意事项
MongoDB作为一个基于分布式文件存储的数据库,近两年大受追捧.数据灵活的存取方式和高效的处理使得它广泛用于互联网应用. 最近本人开始在Windows 32位平台下研究MongoDB的使用,为了方便 ...
- mysql 8.0.11 安装(windows)
mysql本地安装(windows) 一.安装包下载 从官网下载安装包,地址:https://dev.mysql.com/downloads/mysql/ 二.配置 解压到本地,然后在目录下新建my. ...
- ElasticSearch安装为Windows服务
目前我都是在windows的环境下操作是Elasticsearch,并且喜欢使用命令行 启动时通过cmd直接在elasticsearch的bin目录下执行elasticsearch 这样直接启动的话集 ...
随机推荐
- 使用-solidity-开发第一个-以太坊智能合约
目录 目录 使用 solidity 开发第一个 以太坊智能合约 前言 项目源代码 最终效果 环境搭建 智能合约内容 Truffle 创建项目 Truffle 编码 Truffle 打包 Truffle ...
- Typecho框架个人博客搭建方法学习
使用Typecho框架一个月又十二天了,就目前感觉来说,整体还不错,很多方面都支持个性化,二次开发,但是目前MD编辑器有一丢丢问题,不能同步滚动条滚动,就是编辑器区域滚动,预览区域没有动静,需要两边都 ...
- Nuxt.js 应用中的 build:error 事件钩子详解
title: Nuxt.js 应用中的 build:error 事件钩子详解 date: 2024/11/7 updated: 2024/11/7 author: cmdragon excerpt: ...
- docker连不上私有仓库Harbor
解决办法: # 配置多个host(配置本地域名映射) [root@vm10-11-0-38 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.local ...
- 模态内重叠优化,简单有效的CLIP微调方法 | BMVC'24 Oral
来源:晓飞的算法工程笔记 公众号,转载请注明出处 论文: CLIP Adaptation by Intra-modal Overlap Reduction 论文地址:https://arxiv.org ...
- 联邦学习开山之作Communication-Efficient Learning of Deep Networks from Decentralized Data
1 介绍 1.1 背景 越来越多的手机和平板电脑成为许多人的主要计算设备.这些设备上强大的传感器(包括摄像头.麦克风和GPS),加上它们经常被携带的事实,意味着它们可以访问前所未有的大量数据,其中大部 ...
- python数据结构的性能分析
2.python数据结构的性能分析 一.引言 - 现在大家对 大O 算法和不同函数之间的差异有了了解.本节的目标是告诉你 Python 列表和字典操作的 大O 性能.然后我们将做一些基于时间的实验来说 ...
- Java 编程的动态性,第 1 部分: 类和类装入
研究类以及 JVM 装入类时所发生的情况 这一有关 Java 编程动态方面的新的系列文章研究了执行 Java 应用程序时幕后所发生的事情.企业 Java专家 Dennis Sosnoski 提供了 J ...
- One API 替代品 Chat Nio 安装与使用教程
有这样一位初中生,他在初一下学期发起了一个项目,专门用来给他的朋友们免费体验 GPT 模型. 到了八年级的暑假,他决定把这个项目开源出来,并且正式命名为 Chat Nio,同时项目的定位为一站式 LL ...
- C++ Builder 开发64程序 使用AnsiString的ToInt和ToDouble会内存泄漏
AnsiString str="adsfaga"; try { int v=str.ToInt(); } catch(...) { } 上面的代码,在C++ Builder 10 ...