Running a python script as a windows service after converting to exe - python

I have a python script which is raising a socket for communication purposes, I've managed to convert it to exe using Pyinstaller and run it successfully by double clicking on it.
I tried to make the exe a windows service using the following guide but when I ran the service through services.msc, I got:
Windows could not start the test1 service on Local Computer
Error 1053: the service did not respond to the start or control request in a timely fashion
I wonder whether each python script can realy become a windows service, or it has some criterias , as I didn't see this in the guide above.
I tried to take it more simple ,and created a python script of the following code:
while True:
z = 10
print z
Now I created an exe file and a service, when trying to run it I got:
Windows could not start the test1 service on Local Computer
Error 1067: the process terminated unexpectedly
Can somebody please direct me, what am I doing wrong?

Related

Mac ssh to Windows PowerShell. Trying to get it to open an app (on the PC). Nothing works, including commands that do it entered directly on the PC

Trying to have a python script open an app and then continue on. I have the following setup:
Windows PowerShell (PS) running on PC.
Mac Terminal running bash. ssh... into the PC.
In both windows I get the same "Windows PowerShell, Copyright (C) Microsoft..." etc. info and the same PS C:\... prompt. So it seems I'm in the right place.
Tried various things. In all cases, anything I do directly on the PC that successfully opens the app, when done at the same prompt in the ssh session on the Mac, gives all the same feedback, but doesn't actually open the app.
Things I've tried:
py .\open_mt5.py at the PS prompt, where that script is:
import subprocess
result = subprocess.Popen(['C:\\Program Files\\MetaTrader 5\\terminal64.exe'])
print(result)
py to start python REPL, then enter the above lines one by one. Notably, in both setups, the python REPL prompt becomes available immediately (as expected) and the output is the same, but again, the app doesn't open on the PC.
Just execute the app from the PS prompt: C:\'Program Files'\'MetaTrader 5'\terminal64.exe
...and a few other things.
I think this should work, but it doesn't. What am I missing?
Or, speaking to the end goal specifically: how can I have my python script, run via ssh, open that app on the PC (and of course not have that block the script from continuing)?
Thanks!
Couple of other details if it helps:
It seems the hiccup is somewhere in the ssh, since that's the difference in all cases. But it also seems to be a Windows thing, because if I ssh into another Mac and enter commands to open Mac apps, they open on the remote Mac, (as expected).
Everything else I've ever tried via ssh from the Mac (to a Mac or a PC) works the same as it does typing directly into the same prompt on the remote machine, including running an entire python based REST API server on the PC. So I'm pretty sure everything is configured more or less right.
The app in question needs to be running for my REST API to work as it includes a service that provides some of the functionality. My end goal is have the python script open the app at the start of everything it's doing to set up and run the REST API server, and do so without blocking the rest of the script of course.
New to python. Self-teaching. Desire to learn it (basics at least). New to Windows command line (cmd, PowerShell). Self-teaching. No desire to learn any more than required for specific project. (Mac/unix user normally). Hoping people can reply at that level. 😊
Mac: macOS Monterey 12.4. PC: Windows 10 Home 21H2, python 3.10.5, and the default Win 10 OpenSSH Server.

Run Python Script (On Network Drive) as Windows Service with NSSM

I have followed around 5 tutorials on using NSSM to run a python script residing on a network drive. It creates the service, I can edit the service but when I start the service I get the following error:
Unexpected status SERVICE_STOPPED in response to START control
When I try starting the Windows-10 service from Services I get the following error:
Services error during start
I have changed the path to the python script from the full network path to a mapped network path and that did not change anything.
I have also tried using Task Scheduler which worked once in a while and was intermittent and I also tried the pywin32 method as posted here but it fails to start the service as well.
I figured it out. On my remote VM it does not allow Local System Account to be set as the Log on. It required me to add an admin user name and password. It's running now.

How to always run specific python script on windows server manager 2016

i am new to the community and new in using servers and could use some help.
I am trying to setup an automatic JSON parser to another server using http post calls. The idea is as follows:
I manually put JSON files into a folder Input on the server
A python script that is always running on the server reads the files located within the folder
It reads the JSON files, posts all objects to another server, and moves the files to a "Processed" folder one file at a time.
I have been given a Windows Server, with Windows Server Manager 2016, and have managed to do the following:
installed python 3.8.2. on the windows server
able to run a python script using powershell
Installed NSSM to create a windows service
Now the windows server manager says i cannot resume or start the service that i tried to install via NSSM.
I am very new to servers, as well as python itself. Can somebody help me to get a python script running 24/7 on a windows server with windows server manager 2016?
Edit:
I managed to create a python script that can read files, upload them and move them to a processed folder, but i still have to run it by myself while i want it to always run on the server

Is there a way run a python script on another server?

I have a Djnago web application on IIS 6 on one server. Is there a way that from this website I call another python script that is on another server in such a way that that script just run itself there?
Calling or Runnig that script in the usual way as internet says, is not working.
I always get the error of os.getcwd() and it also doesn't allow to change that directory.
I just want to run that python script there on that server from this server.
Can anyone help?
Normally, I would recommend using a framework like fabric or winrm if you want to run a python script on another server. Those frameworks use ssh or windows remoting functionality, respectively, to allow a python program to execute other commands (including python scripts) on other systems. If the target machine is a windows machine, be forewarned that you can run into all sorts of UAC issues doing normal operations.

How do I create a windows service with Python

Merged with How do you run a Python script as a service in Windows?.
I really need my python program to run as a windows service. How can I do this?

Categories