Starting a GUI process from a Python Windows Service - python

I am creating Windows service class in Python that will eventually display a Window when certain conditions are met. Since (as I understand it) services cannot have GUIs, I'm trying to start up a GUI in a seperate process (using subprocess.Popen) when the conditions are right. This isn't working, presumably because the child process has the same privileges as the service.
So how do I start a process from a Python Windows Service that has the ability to display GUIs on the screen?

If you give your Service the Allow service to interact with desktop permission it will be able to create windows without the need to launch a subprocess.

As mentioned in this answer, you may have to (eventually) transition to a client-server model since Windows Vista and later no longer support direct interaction with users.

Related

Launching a Windows task using Python event?

I created a Python script that opens a certain software on my PC (the MetaTrader 5 software to be specific), does some processing, saves a file to disk, and closes the software. To have the script running in the background (in hidden / headless / background mode), I was advised to convert the script to a Windows task, since my initial approach using subprocess.Popen() and the STARTUPINFO instance seems to fail for any complex application (see John Hennig's comment under his reply here). Therefore, I created a .bat file and connected it with my new Windows task. Now I hit a roadblock though, since Windows tasks are typically set up with triggering time/frequency to be run automatically. In my case, however, I want the trigger to be an external Python script. Is this possible? If not, is there any other way to have applications run without their windows flashing up on screen?

What are the access rights required for a Python program to run as a service?

I have a few personal helper applications written in Python that communicate with me either through the Windows 10 taskbar area on the right (where the icons are - mine below is the red and blue square), or the Windows 10 Notification Center.
They currently run in a minimized shortcut (that points to python.exe) and I would like to remove them from the taskbar
The solution I am looking at is to run them as services. I created one (through nssm) and started the service that runs my program (via the Local Service account, which allows me to (hopefully) interact with the desktop). Unfortunately, I get
I allowed MY-COMPUTER-NAME$ to access the working directory of the code, as well as to the script itself.
Is there something else I should allow for the script to successfully run?
(Note: the service started when I used my account, but the taskbar icon started from my program did not show up)
I once used this command in cmd to run a service that is designed in python. follow this
sc create WojAPP binPath= "C:\Python34\Python.exe -- C:\tmp\yourpythonscript.py"
[reference]When creating a service with sc.exe how to pass in context parameters?

Automation Scripts Fail when RDP(VM) is minimized

I have been facing issue with automation execution of my script on one of the VM. I have automated the functionality of Saving a Document which is ideally a Windows Designed UI. I have tried using various technologies/tools like AutoIT, Python, Sikuli but the script halts if VM is minimized. It works perfectly fine is VM is open via RDP and I can see runtime execution. But If I minimize the RDP, the script halts at 'Save As' dialog box, none of the send keys (Cntrl+s) or (Enter) work via AutoIt script. Please help with some solution so as to have successfully execution of script even in minimized mode.
The reason why your script fails when it gets executed over a minimized RDP session is quite simple. GUI automation/testing tools need to have an unlocked, active desktop - otherwise the operation system thinks that it doesn't need to actually render GUI operations (which is time consuming) since there no user can that can see the rendered graphical user interface anyway. And programs don't communicate via GUIs normally ...
This is why QF-Test and other GUI automation/testing tools often have a note in their FAQs describing this kind of problem. For example FAQ 14 in the case of QF-Test, see https://www.qfs.de/qf-test-handbuch/lc/manual-en-faq.html
As described in the FAQ 14 on Windows 10 or Windows Server 2016 and in case of an RDP connection you need to modify the Registry. Go to
HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client
and add a new value
RemoteDesktop_SuppressWhenMinimized as DWORD having the value 2
After restarting you will then be able to minimize the RDP connections. However disconnecing or closing the RDP connection will probably still result in a failure.
You could try running tscon.exe RDP-Tcp#0 /dest:console as admin as mentioned here. This will disconnect your RDP connection but should leave all GUI programs running normally on the VM. I have personally used this with autoit on a VM and it worked OK. Of course you will not be able to monitor your script as it runs so this may or may not work for you.

How do I limit a Ubuntu machine to a Python GUI?

I wrote a python GUI in Tkinter for a time-clock system. The micro machine is wall mounted and the employees only have access to the touchscreen menu I programmed and a barcode swipe. I know how to get the script to start on startup, but how do I prevent them from exiting out or opening other menus? Basically the sole purpose of this console is to run the time-clock GUI.
If it cant be done in Ubuntu, is there another flavor of linux it can be done in?
There is also de KDE Kiosk project, which you can install on a ubuntu machinne:
"
The KDE Kiosk is a framework that has been built into KDE since version 3. It allows administrators to create a controlled environment for their users by customizing and locking almost any aspect of the desktop which includes the benign such as setting and fixing the background wallpaper, the functional such as disabling user log outs and access to the print system and the more security conscientious such as disabling access to a command shell."
http://techbase.kde.org/KDE_System_Administration/Kiosk/Introduction
You can use wm_overrideredirect, then make the UI full screen. This will remove all window decorations so there's no way to close the window. If that's not enough, as a final step you can do a global grab. With that, you effectively control everything that they can do.
Be very careful about coding global grabs -- make sure you can ssh into that box to kill the process, otherwise you can effectively denial-of-service your box.
Don't start a window manager. Only start your program, e.g. from xinitrc. Make the program full-screen

How do I make a Python script (installed as a service) survive a logout?

I followed the instructions in this answer about writing a Python script to be used as a service. I placed my looping code in def main().
I installed the service with python my_script.py install. I was able to Start and Stop the service through services.msc in Windows XP.
It's a logging program that is intended to write logs as long as Windows is running. It shouldn't care about who or whether anyone logs in or out. My problem is that the service will stop when I logout. How do I make it survive logouts?
The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off, or shuts down the system so that the process has an opportunity to clean up before termination. To ensure that you detach your service from all this, you will need to use the control handler to set it that way:
win32api.SetConsoleCtrlHandler(lambda x: True, True)
Check out : http://msdn.microsoft.com/en-us/library/ms685049(v=VS.85).aspx
Just checked out that there is recipe that illustrates this very well for you.
http://code.activestate.com/recipes/551780-win-services-helper/

Categories