简体   繁体   English

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

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

Running a python script (developed in an other department) by a C# Windows Service application.由 C# Windows 服务应用程序运行 python 脚本(在其他部门开发)。

I wrote a console application which executes a python script.我编写了一个执行 python 脚本的控制台应用程序。 Now I tried to transform the console app into a Windows Service.现在我尝试将控制台应用程序转换为 Windows 服务。 The Windows Service is working without the script propper (loggin in event log). Windows 服务在没有脚本属性的情况下工作(登录事件日志)。 The service stopped at the point of starting ProcessStartInfo.服务在启动 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;
                }
            }
        }

Thank you to @BugFinder for his support.感谢@BugFinder 的支持。 At the end it was a spelling mistake.最后是拼写错误。

Example solution:示例解决方案:

First project: Logic.csproj第一个项目: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;
    }
  }
}

Second project: ScriptRunner.csproj第二个项目: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());
    }
  }
}

Third project: ScriptRunnerService.csproj第三个项目: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);
    }
  }
}

Install Service:安装服务:

You need InstallUtil.exe to install the service!您需要 InstallUtil.exe 来安装服务!

installutil -i ScriptRunnerService.exe installutil -i ScriptRunnerService.exe

Windows-Key: r services.msc -> Start Service eventvwr.msc -> Check Logs Windows 键:r services.msc -> 启动服务 eventvwr.msc -> 检查日志

Uninstall Service:卸载服务:

installutil -u ScriptRunnerService.exe installutil -u ScriptRunnerService.exe

Finde complete solution incl.查找完整的解决方案,包括。 python script on GitHub GitHub 上的python脚本

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

相关问题 在Windows Phone 8中运行Python脚本/应用程序 - Running Python Script/application in Windows Phone 8 如何从Windows 8中的服务获取登录用户 - How to get logged in user from a service in windows 8 登录MVC应用程序之前验证Windows用户 - Validate windows user before logged in MVC application 登录用户而不是运行程序C#,Windows的用户 - Getting logged in user not the user running the program C#, Windows 如何获取以用户身份运行的Windows 7应用程序的MainWindowHandle <foo> 从作为本地系统运行的服务中? - How can I get the MainWindowHandle of a Windows 7 application running as user <foo> from within a service running as Local System? 如何在Windows服务中查看Windows窗体应用程序作为服务运行的应用程序 - How to View the Windows form application In Windows Service Application running as service 如何在C#服务应用程序中指定是否登录窗口? - How to specify whether the windows is logged in or not in C# service application? Windows服务可以在没有登录的情况下启动应用程序吗? - Can a windows service start an application without being logged in? HttpClient和Windows Auth:将用户消费者登录到服务 - HttpClient & Windows Auth: Pass logged in User of Consumer to Service Windows Service从登录的用户c#启动 - Windows Service start from logged in user c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM