繁体   English   中英

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

[英]Running a python script in a windows service application (no user logged on)

由 C# Windows 服务应用程序运行 python 脚本(在其他部门开发)。

我编写了一个执行 python 脚本的控制台应用程序。 现在我尝试将控制台应用程序转换为 Windows 服务。 Windows 服务在没有脚本属性的情况下工作(登录事件日志)。 服务在启动 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;
                }
            }
        }

感谢@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 services.msc -> 启动服务 eventvwr.msc -> 检查日志

卸载服务:

installutil -u ScriptRunnerService.exe

查找完整的解决方案,包括。 GitHub 上的python脚本

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM