Python - Win32Com Client Dispatch Hanging as Scheduled Tasks - python

I have a Python Script, which uses win32com.client.dispatch and redemption in order to connect to an instance of Outlook and harvest some data from a public folder.
When I execute this script on command line it works just fine.
Adding it as a scheduled task, it appears to get hung at the line Outlook = win32com
I added Event Log statements along the way to see where it is getting hung, other than that I don't have much in the way of error logs (since it doesnt actually fail)
Is there any sort of security settings I should be concerned about or anything I am not thinking of? Everything works fine with a standard python call in the CMD.

Scheduler runs as a service, and office apps (Outlook included) should nto be used in a service.

Related

Python script not properly executing using Windows Task Scheduler

Created a Python script that gets data from SQL Server, then writes the data to an Excel spreadsheet using Pandas and xlsxwriter. Then, the same script uses the win32 client library to send the newly created file via email from my Outlook address to certain recipients.
Now, since I need this to be fully automated, I attempted to have Windows Task Scheduler try to do this at a scheduled time. It creates the Excel sheet properly, but it fails to send the email. Oddly enough, if I run the Python script through a Python IDE, the entire process executes successfully without issue. I have verified that the email was sent and that I received it. The email neither sends nor do I receive it when using WTS.
Are there any work-arounds for this? Should I use a 3rd party task scheduler instead?
The Windows Task Scheduler has some problems, because you will probably not have enough rights to send an email. Try to manually deactivate and again activate the scheduled task in the WTS-program as an administrator, if that does not work, you will have to take a 3rd party task scheduler.

Is it Possible to Run a Python Code Forever?

I have coded a Python Script for Twitter Automation using Tweepy. Now, when i run on my own Linux Machine as python file.py The file runs successfully and it keeps on running because i have specified repeated Tasks inside the Script and I also don't want to stop the script either. But as it is on my Local Machine, the script might get stopped when my Internet Connection is off or at Night. So i couldn't keep running the Script Whole day on my PC..
So is there any way or website or Method where i could deploy my Script and make it Execute forever there ? I have heard about CRON JOBS before in Cpanel which can Help repeated Tasks but here in my case i want to keep running my Script on the Machine till i don't close the script .
Are their any such solutions. Because most of twitter bots i see are running forever, meaning their Script is getting executed somewhere 24x7 . This is what i want to know, How is that Task possible?
As mentioned by Jon and Vincent, it's better to run the code from a cloud service. But either way, I think what you're looking for is what to put into the terminal to run the code even after you close the terminal. This is what worked for me:
nohup python code.py &
You can add a systemd .service file, which can have the added benefit of:
logging (compressed logs at a central place, or over network to a log server)
disallowing access to /tmp and /home-directories
restarting the service if it fails
starting the service at boot
setting capabilities (ref setcap/getcap), disallowing file access if the process only needs network access, for instance

sending mouse/keyboard events on remote desktop

So I am trying to set up a Continuous integration environment using Jenkins.
One of the build step requires a series of mouse actions/movements to accomplish a task in Excel. I have already written a python script using the ctypes library to do this.
The script works perfectly fine if I run it either through Jenkins or on the server itself when I am actively logged in to the server using remote desktop connection, but as soon as I minimize/close the connection and then run the script from Jenkins, it seems the mouse events never get executed. Is there something I can add to the script to make this work? Thanks for any help you can provide.

Python Script doesn't work properly as Geoprocessing Service

I have banged my head against the wall on this for days now, I need help.
I’m trying to create a geoprocessing service (which will be used via a flex viewer) that takes permit information from a GDB, writes it to a file, and then opens it on the user’s computer.
The script I have written works perfectly on local computers and when run from the server itself.
However, as soon as the script has been published as a geoprocessing service, it stops opening the file.
The file is created out on a webserver, and then opened via url so that the user can then make a local copy, print out for signature, etc. The file is still being created, named, and placed in the proper folder, it just isn’t being opened at end of the process. (The GP Service views as completed though, it doesn’t fail)
Here is the end of the code, which is the part relevant to my issue:
f = open(r'\\MyWebAddressHere\\Reports\\Permit' + str(PermitNumber) +'.html','w')
f.write(permitStart)
if (BasePermitFee > 0):
f.write(permitBaseFee)
if (PlumbingFee > 0):
f.write(permitPlumbingFee)
if (ElectricFee > 0):
f.write(permitElectricFee)
if (TotalFees > 0):
f.write(permitTotalFee)
f.write(permitEnd)
f.close()
webbrowser.open('MyWebAddressHere/Reports/Permit' + str(PermitNumber) +'.html')
Does anyone have a suggestion on why the open is failing once the GP service is published?
I don't see this as a bug - the webbrowser command makes no sense in the context of a GP Service. Think about what's being executed where.
If you run the code locally, Python is running on your actual computer - that computer has a login, a display, a set of installed programs. Telling it to open a web browser makes sense because you have one or more browsers installed on that computer.
Run it on the server, while logged into the server, and same thing - you still have a login, a set of installed programs. While you might lock down a server to reduce its vulnerability, it probably still has at least one program installed that is capable of browsing the internet.
But when you run a script as a GP Tool, you're in a different world. You're not running the code locally, and you're not logged into the server. If you execute the webbrowser command, where should it open the browser window?
On your PC? It can't - the server's GP environment can't execute random programs on your PC, that would be a massive security hole. Hey, I'll write a script that deletes everything in C:\Windows and publish it on ArcGIS Online!
On the server? Who would see it? No one's logged in.
Turns out that the webbrowser command flat out does not work in a server environment.
I managed to confuse the heck out of an ESRI server analyst with this one, because there is apparently no documentation saying this won't work in a server environment, it just doesn't. So it has been logged as a bug.

How to launch a python process in Windows SYSTEM account

I am writing a test application in python and to test some particular scenario, I need to launch my python child process in windows SYSTEM account.
I can do this by creating exe from my python script and then use that while creating windows service. But this option is not good for me because in future if I change anything in my python script then I have to regenerate exe every-time.
If anybody have any better idea about how to do this then please let me know.
Bishnu
Create a service that runs permanently.
Arrange for the service to have an IPC communications channel.
From your desktop python code, send messages to the service down that IPC channel. These messages specify the action to be taken by the service.
The service receives the message and performs the action. That is, executes the python code that the sender requests.
This allows you to decouple the service from the python code that it executes and so allows you to avoid repeatedly re-installing a service.
If you don't want to run in a service then you can use CreateProcessAsUser or similar APIs.
You could also use Windows Task Scheduler, it can run a script under SYSTEM account and its interface is easy (if you do not test too often :-) )
To run a file with system account privileges, you can use psexec. Download this :
Sysinternals
Then you may use :
os.system
or
subprocess.call
And execute:
PSEXEC -i -s -d CMD "path\to\yourfile"
Just came across this one - I know, a bit late, but anyway. I encountered a similar situation and I solved it with NSSM (Non_Sucking Service Manager). Basically, this program enables you to start any executable as a service, which I did with my Python executable and gave it the Python script I was testing on as a parameter.
So I could run the service and edit the script however I wanted. I just had to restart the service when I made any changes to the script.
One point for productive environments: Try not to rely on third party software like NSSM. You could also achieve this with the standard SC command (see this answer) or PowerShell (see this MS doc).

Categories

Resources