Throw error on task scheduler when python script throws error - python

I have a python script. I have scheduled it to run monthly on Windows scheduler. However, it is prone to error sometimes and I would like to know if it threw an error. However the windows scheduler always shows the status as Successful. How can I make the Windows scheduler display the error or notify me if my .py script throws an error?

I don't believe you can show the information in Windows scheduler because it calls ShellExecuteEx() to run your program and that will report success no matter what return code your Python program issues. That is because the scheduler has successfully run the Python interpreter, which is all it cares about.
Have your program write the error to the Windows event log and look for it there. You can get fancy and set up the event log to notify you.

Related

Windows Task Scheduler Last Run Result (0xF6)

What does Last Run Result (0xF6) mean?
I scheduled the Task Scheduler to run a *pythonw.exe with the arguments pointed to the *.py file.
User account is set for an admin account
Run whether user is logged on or not
Run with the highest privileges
Configured for Windows 10
I can't find a reference to this in a google search. Whatever is happening via the Task Scheduler I can't really tell because the python script does not finish-- meaning I can't read the log file to check how far it got or if it ran into any errors. If I run the script without the Task Scheduler it completes successfully.

Windows service works when running through command line but not through Services

Following this guide I created a service from a python script and used "py [script name] install". The service was installed successfully and I can see it in the services window. If I run the service through the command line using "py [script name] start", it all works correctly.
However, when I try to start it from the Windows Services window, it gives a different error depending on the account I use. All of these are suggestions I've seen on various forums:
1) Default (Local System Account)
When I try this one, it gives error "Windows could not start the service on Local Computer."
2) The admin account, which I'm running the cmd from
Same error as above
3) LOCAL SERVICE
Gives "Error 5: Access is denied." even though I'm logged in as admin.
Does anyone know what could be causing this?
I also tried creating a .exe from the python script and adding that as a service using sc.exe. However, when I did that, I never got past "The service did not respond to the start or control request in a timely fashion." This error also occurred when running it from the CMD until I copied some files into a directory in the python installation.
I've never tried a python script as a service, so I can't speak to that directly.
I do know that the "The service did not respond to the start or control request in a timely fashion." error is almost always an indication that the OnStart() callback to the service (looks like SvcDoRun in your case) starts a long running operation (e.g., while loop) that never returns. The OnStart() callback is expected to kick off a foreground thread that will keep the service running and then return. From my recollection, if OnStart() doesn't return in about 30 seconds or so, Windows kills the service as unresponsive, which trigger this particular error message.
Another thing to look at is the event viewer. It may be that your service is throwing an exception that you're not seeing due to process isolation.

Pyinstaller *.exe in TaskScheduler

i created a small python program and freezed it to an *.exe file with the --noconsole option. When I run the program normally it works. But when I put it in the task scheduler that runs it on startup it does not work anymore.
The program has no ui. I know that it is not working because it is not writing to a log file. The paths in the program are absolute.
Any ideas what can cause this problem?
You will need to do a little troubleshooting to narrow down the problem. I can share some tips.
Make sure you have an exception handler for uncaught exceptions (at the very top) so you are logging those errors to a file. If you don't have one you will never know why your program is crashing. Make sure if you add this you are re-throwing the exception after logging it so the os knows it crashed.
Open the task schedule and run the task manually. Make sure the task is allowed to be ran manually (it's in the configuration). If it is allowed to run manually, then it should have no trouble running automatically.
The user you're running the task with might be different from the user you are manually running it with. You can try removing the --noconsole option while you are trying to figure out the problem. If it is an issue with your logger not having write access where it needs to, you would not see anything in your logs but can see messages in the console.
Make sure the working directory of the task is correct. By default it should use the same directory the app is in, but if a path has been entered it can cause issues.
In addition to Timothy's ideas, make sure the option Run whether user is logged or not is activated, and if necessary, put the user password (when applying).
When you run a task on startup, the Windows session is not yet opened so the task may not run if Windows has no info how to open a session for it.
I was trying to get information of Duplicati (Backup Software), but the server does start only when somebody is logged in. So the problem was not my program but the start of this other program.
Solved it through logging my program.

Python Script Running in Windows Task Scheduler Completes with exit code 3221225477

I'm troubleshooting a python script that does 3 things.
Stops ArcGIS Server using subprocess.check_output('net stop "ArcGIS Server"', shell=True)
Runs some arcpy functions
Starts ArcGIS Server using subprocess.check_output('net start "ArcGIS Server"', shell=True)
The script runs fine when run from IDLE. It also runs fine as a scheduled task when I check the radio button that says "Run only when the user is logged on"
However, this task runs on a server and I don't want to have to be logged in for the task to run. So I check the radio button that says "Run whether user is logged on or not", and "Run with highest privileges".
The result is the script's log file is empty, and the script never completes. It returns the error code in the Task Scheduler, that I mentioned above.
More task details:
Action: Start a program
Program/script: C:\Python27\ArcGIS10.3\python.exe
Add arguments (optional): D:\ArcGISData\server-data\Python\copydata.py
Am I doing something wrong, or is there any other steps I can try to get this working?
Not sure why it wasn't working to be honest, but after going through the steps to recreate the task, it started working. Maybe I had just mistakenly entered the credentials of the user running the task or something like that.

Python script involving Outlook through win32com runs when double-clicking, but not through task scheduler

I have a python script that sends out an email using win32com and Outlook. The script runs without a hitch when I run it through an interpreter or double-click on the script. However, when I run it through the Task Scheduler, I get the following message:
"Something went wrong. We couldn't start your program. Please try starting it again. If it won't start, try repairing Office from 'Programs and Features' in the Control Panel."
I'm using Office 365, and Python 2.6. I've tried running the script through the scheduler after killing the Outlook process, but I ran into the same issue.
Office isn't designed to run as a service, and needs to be run interactively. You'll need to change your task configuration in Task Scheduler to run the task as the currently logged-in user, on the current user's desktop, with the current user's privileges.

Categories

Resources