简体   繁体   English

从Linux服务启动GTK#应用程序

[英]Launch a GTK# application from a Linux service

Environment : 环境
RHEL 5.7, 64-bit RHEL 5.7,64位
runlevel 5 运行级别5
mono 2.6.7 单声道2.6.7

I have a C# console application that is run as a service on startup. 我有一个C#控制台应用程序,它在启动时作为服务运行。 From that application's code, it launches two processes that are also C# applications. 通过该应用程序的代码,它启动两个也是C#应用程序的进程。 One is another console app, and one is a GTK application (but is set to not show its GUI window). 一个是另一个控制台应用程序,一个是GTK应用程序(但设置为不显示其GUI窗口)。

Running this main app normally (mono app.exe) works fine. 正常运行此主应用程序(mono app.exe)可以正常运行。 However, when running it as a service, the GTK application crashes at Application.Init() with a "GTK-cannot open display" error message. 但是,当将其作为服务运行时,GTK应用程序在Application.Init()处崩溃,并显示“ GTK-无法打开显示”错误消息。

That error message is also what we see when we try directly running the GTK app (aka "mono appname.exe") in runlevel 3 (assuming because Application.Init() does something with the window manager that is not there). 当我们尝试在运行级别3中直接运行GTK应用程序(也称为“ mono appname.exe”)时,也看到了该错误消息(假设因为Application.Init()对不存在的窗口管理器做了一些操作)。 So does a linux service somehow start in runlevel 3, even though we start the machine in runlevel 5? 那么,即使我们在运行级别5中启动计算机,Linux服务也是否会以某种方式在运行级别3中启动? And if so, is there a way I can force it to run at runlevel 5? 如果是这样,是否有办法强制我使其在运行级别5运行?

Thank you. 谢谢。

EDIT - more information: The GTK application is set up so that it can run with or without launching its GUI via a command line parameter. 编辑-更多信息:GTK应用程序已设置为可以通过命令行参数在启动或不启动GUI的情况下运行。 However, we are using Gtk.Application.Run() to start the application from main no matter which way it's running. 但是,无论使用哪种方式运行,我们都使用Gtk.Application.Run()从main启动应用程序。 Gtk.Application.Init() must be called before Gtk.Application.Run(), and it tries to do something with the x server - which obviously causes a problem in runlevel 3. However, our machine will always be started in runlevel 5. 必须在Gtk.Application.Run()之前调用Gtk.Application.Init(),并且它会尝试使用x服务器执行某些操作-这显然会导致运行级别3出现问题。但是,我们的计算机始终会在运行级别5中启动。

The main app that launches the other two is setup as a service because the overall system it's going on requires it to start as a service at boot time. 启动其他两个应用程序的主应用程序设置为服务,因为正在运行的整个系统要求它在引导时作为服务启动。 So, is there a way to run it in a user-interactive mode? 那么,有没有办法在用户交互模式下运行它? (even though they won't actually be interacting with it directly) (即使他们实际上不会直接与其进行交互)

You are not going to be able to do this easily without dividing your program into two or more parts. 如果不将程序分为两个或更多部分,您将无法轻松地执行此操作。

When started as a service during boot your program will either be running as root or another nominated user. 在启动过程中作为服务启动时,您的程序将以root或其他指定用户身份运行。 It will not have an associated X session ( X may not have started either ). 它将没有关联的X会话(X也可能没有开始)。

When the GTK main loop (via Application.Init) starts it tries to contact the X server which it wont be able to find or if it does may not have permission to use. 当GTK主循环(通过Application.Init)启动时,它将尝试联系无法找到的X服务器,或者可能没有使用权限。

You should split your program into a background service and a GUI, cramming the two in the same thing has development drawbacks aswell. 您应该将程序拆分为后台服务和GUI,将两者混为一谈也存在开发缺陷。

Also, presumably, your service has some sort of client program? 另外,大概您的服务有某种客户端程序? Why can your GUI not be a client also and launched separately? 为什么您的GUI也不能同时作为客户端并单独启动?

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

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