[英]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.