Dear Reyhan Prasetyo,
Hello! Welcome to the Microsoft Community. I'm glad to be able to assist you. If I misunderstand your question, please let me know promptly.
It sounds like a challenge to know that you have a problem running Python scripts with Task Scheduler. I understand the confusion caused by this problem, and I will try my best to help you solve it.
According to what you mentioned, the task scheduler can work normally at startup, but there is a problem in the subsequent operation and the event viewer displays the exception code 0xc0000409, which usually indicates that some kind of access violation, stack buffer overflow or crash has been encountered. This may indicate that there is a problem with your script itself, or there is a problem with the environment in which the task scheduler runs when it is triggered. You can try the following methods, hoping they can help you:
1.Check the script running environment
- Ensure that Python is installed correctly: Ensure that the Python path used in the task scheduler -matches the installed Python version.
- Use full path: In the Program/Script field, make sure that the full path of the Python executable file is specified, for example, c: \ Python39 \ python.exe.
- Specify the full path of the script: in the Add Parameter field, pass the full path of your script, for example, c: \ path \ to \ your \ script.py.
2.confirm the Start In parameter:
Make sure to fill in the full path of the script directory in the "Start In" field, that is, the folder where your script is located. For example: C:\path\to\your.
3.Make sure the permissions are set correctly:
- Even if it is set to "Run with Highest Privilege", please ensure that all resources (files, networks) used in this script have appropriate privileges for the user to access.
- Check folder and file permissions. Make sure that the account running this task has access to these files or directories.
4.Add exception handling to the script: modify your Python script to ensure good error handling, such as using try-except blocks to catch and record exceptions. You can add logging to the script to help debugging.
5.Test Task Scheduler
- Separate testing: a way to run Python scripts separately (at the command prompt) to ensure that they work properly and avoid isolating problems.
- Simplify the scheduler configuration: When testing, you can temporarily remove the complex configuration, such as setting the trigger only once instead of repeating it every hour, which makes it easier to debug the problem.
6.Check more details in Event Viewer: In Event Viewer, check the detailed log corresponding to this task for details that may cause the crash.
7.Check resource usage: Make sure that all resources and libraries in the script are available at each run. For example, check to make sure that the network connection is normal if you rely on network requests.
8.Compatibility: Make sure that the Python version and all libraries you use are compatible with your scripts and Windows.
The above methods are some common solutions, and you may need to analyze the specific situation, because the problems may be different based on different environments.
I hope the above information can be of help to you. If you try the solution, please feel free to tell me the results or the problems you encounter.
Best Wishes
Pyked | Microsoft Community Support Specialist