quartz.net .netcore 定时任务部署到linux
1.因为linux进程就是类似于服务
所以topshelf 创建的部署到window比较方便
部署到linux 直接创建.netcore workservice 项目,然后将quartz.net 按照部署就可以。
如果不用workservice 用控制台程序的话,要阻塞主线程,因为控制台运行完成后就会关闭。
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using NLog; using Quartz; using Quartz.Impl; using Quartz.Spi; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace LinuxDiskServer { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService<QuartzNetService>(); }); public class QuartzNetService : IHostedService { private readonly ILogger<QuartzNetService> _logger; IScheduler _scheduler = StdSchedulerFactory.GetDefaultScheduler().Result; public QuartzNetService(ILogger<QuartzNetService> logger) { _logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { try { await _scheduler.Start(cancellationToken); _logger.LogInformation("Quartz调度服务开始"); } catch (Exception ex) { _logger.LogError("Quartz调度服务启动失败" + ex.Message); } } public async Task StopAsync(CancellationToken cancellationToken) { _logger.LogInformation("定时器结束工作"); await _scheduler.Shutdown(cancellationToken); } } } }
quartz.net .netcore 定时任务部署到linux
原文:https://www.cnblogs.com/elsons/p/15340078.html