在windows服务应用程序中运行python脚本(没有用户登录)

2024-09-30 10:40:12 发布

您现在位置:Python中文网/ 问答频道 /正文

运行一个由C#Windows服务应用程序开发的python脚本

我编写了一个执行python脚本的控制台应用程序。现在我尝试将控制台应用程序转换为Windows服务。Windows服务在没有脚本propper(事件日志中的loggin)的情况下工作。服务在启动ProcessStartInfo时停止

public string Run()
        {
            _pySkript.WorkingDirectory = _workinDirectory;
            _pySkript.FileName = _pythonPath;
            _pySkript.Arguments = string.Format("{0} {1} {2} {3} {4} {5} {6}", a, b, c, d, e, f, g);
            _pySkript.UseShellExecute = false;
            _pySkript.RedirectStandardOutput = true;
            _pySkript.CreateNoWindow = true;
            _pySkript.RedirectStandardError = true;
            _pySkript.RedirectStandardInput = true;
            _pySkript.ErrorDialog = false;
            _pySkript.WindowStyle = ProcessWindowStyle.Hidden;

            using (Process process = Process.Start(_pySkript))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    OnScriptRunFinished();
                    return result;
                }
            }
        }

Tags: 脚本falsetrue应用程序stringwindows事件result
1条回答
网友
1楼 · 发布于 2024-09-30 10:40:12

感谢@BugFinder的支持。最后是拼写错误

示例解决方案:

第一个项目:Logic.csproj

using System;
using System.Diagnostics;
using System.IO;

namespace Logic
{
public class RunScript
{
    string _parameterString = string.Format("{0} {1} {2} {3}","main.py", "Testuser", "TestPw", "MyEnviroment");
    string _resultCon;
    public string Start()
    {
        ProcessStartInfo _pySkript = new ProcessStartInfo();

        _pySkript.WorkingDirectory = @"D:\GitRepos\ScriptRunner\PyScript\";
        _pySkript.FileName = "python";
        _pySkript.Arguments = _parameterString;
        _pySkript.UseShellExecute = false;
        _pySkript.RedirectStandardOutput = true;
        _pySkript.CreateNoWindow = true;
        _pySkript.RedirectStandardError = true;
        _pySkript.RedirectStandardInput = true;
        _pySkript.ErrorDialog = false;
        _pySkript.WindowStyle = ProcessWindowStyle.Hidden;

        try
        {
            using (Process process = Process.Start(_pySkript))
            {
                using (StreamReader reader = process.StandardOutput)
                {
                    _resultCon = reader.ReadToEnd();
                }
            }
        }
        catch (Exception ex)
        {
            _resultCon = ex.ToString();
        }
        return _resultCon;
    }
  }
}

第二个项目:ScriptRunner.csproj

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Logic;

namespace ScriptRunner
{
class Program
{
    static void Main(string[] args)
    {
        var runMyScript = new RunScript();
        Console.WriteLine(runMyScript.Start());
    }
  }
}

第三个项目:ScriptRunnerService.csproj

using System.Diagnostics;
using System.ServiceProcess;
using Logic;

namespace ScriptRunnerService
{
public partial class ScriptRunService : ServiceBase
{
    public ScriptRunService()
    {
        InitializeComponent();
        eventLog1 = new EventLog();
        if (!EventLog.SourceExists("MySource"))
        {
            EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";

        var runMyScript = new RunScript();
        var output = runMyScript.Start();
        eventLog1.WriteEntry(output.ToString(), EventLogEntryType.Information);
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("In OnStart.", EventLogEntryType.Information);
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("In OnStop.", EventLogEntryType.Information);
    }
  }
}

安装服务:

您需要InstallUtil.exe来安装服务

installutil-i ScriptRunnerService.exe

Windows键:r 服务.msc->;启动服务 eventvwr.msc->;检查日志

卸载服务:

installutil-u ScriptRunnerService.exe

GitHub

相关问题 更多 >

    热门问题