I've got a process I'm trying to automate through the Windows task scheduler. The file is located right next to the script on a subfolder in my desktop. A mockup that shows the problem I'm running into is:
import os
if __name__ == "__main__":
lines = []
with open(r'C:\Users\user\Desktop\folder\config.txt') as file:
for line in file:
lines.append(line)
with open(r'C:\Users\user\Desktop\folder\output.txt', 'w') as file:
for line in lines:
file.write(line)
When I run this through the command line, the code works fine. The config file is read in properly and the output works properly.
However, in task scheduler, the program fails to execute with an 0x2 error code. In task scheduler, I have the tried with the following task scheduler options selected:
Run only when user is logged on/Run with highest priveledges Run only
when user is logged on/Highest priveledges not checked Run whether
user is logged on or not/Run with highest priveledges Run whether
user is logged on or not/Highest priveledges not checked
I am using absolute paths - as you can see in the above example. I'm also using the "start in" option inside of task scheduler.
This is seriously annoying me.
Edit: I am using absolute paths inside of task scheduler to both the Python executable and the script.
Related
I have made a lot of research on the forum, and still can't figure out how to solve my issue. I am running a Python code from the Windows task scheduler, it can run it but stop at some point. I have create a log file to see where the code stop running and it stops when I am doing an HTTP GET from the requests library.
r = session.get(urlRequest, allow_redirects=True)
The code runs fine on Spyder. Any suggestions?
I created a bat file as well and same issue.
#echo off
"C:\Users\NAME\Anaconda3\python.exe" "C:\Users\NAME\Documents\GTD_scheduledTasks\exchangeRate.py"
pause
In my log files, i asked to print several parameters:
Sys.excecutable C:\Users\NAME\Anaconda3\python.exe
Sys.path
['C:\Users\NAME\Documents\GTD_scheduledTasks',
'C:\Users\NAME\Anaconda3\python39.zip',
'C:\Users\NAME\Anaconda3\DLLs',
'C:\Users\NAME\Anaconda3\lib',
'C:\Users\NAME\Anaconda3',
'C:\Users\NAME\Anaconda3\lib\site-packages',
'C:\Users\NAME\Anaconda3\lib\site-packages\win32',
'C:\Users\NAME\Anaconda3\lib\site-packages\win32\lib',
'C:\Users\NAME\Anaconda3\lib\site-packages\Pythonwin']
os.getcwd() C:\WINDOWS\system32
Thanks!
Edit: I also checked form Spyder where is my ex files for Python and I used the one from sys.excecutable (C:\Users\NAME\Anaconda3\python.exe) both with one \ and with double \
When I go to the event history in the Task Scheduler, i see:
successfully completed: I dont get error return but some statements are not processed (the requests)
actionName is C:\WINDOWS\SYSTEM32\cmd.exe. Not sure if relevant
I have created a new task, with one action: Start a program. I followed the instructions from https://www.jcchouinard.com/python-automation-using-task-scheduler/
my python script looks like this:
if __name__ == '__main__':
num = 1
Under general properties, I have 'run whether user is logged on or not' checked, 'run with highest privileges' checked, and myself as the user running the task. I am an administrator.
Action Parameters...
Program/script: C:\Users\myuser\AppData\Local\Microsoft\WindowsApps\python.exe (pasted from command where python)
Add arguments: py_test.py
Start in: C:\Users\myuser\Desktop
I tested from the command line that I can run this command successfully:
C:\Users\myuser\AppData\Local\Microsoft\WindowsApps\python.exe C:\Users\myuser\Desktop\py_test.py
When I click 'run' from the task scheduler library, I get the error 'The file cannot be accessed by the system. (0x80070780)
When I go into the history for the task, I see this error:
Task Scheduler failed to launch action "C:\Users\myuser\AppData\Local\Microsoft\WindowsApps\python.exe" in instance "{6204cea7-bedc-40f9-bc10-ac95b9e02460}" of task "\TestPythonJob". Additional Data: Error Value: 2147944320.
I confirmed under the executable file's properties that I and SYSTEM have access to it. I tried researching this error value but could not find anything. What could be the issue?
Maybe try adding python to your path environment variables. Then make a .bat to run it. Here's how to use a .bat with task scheduler https://www.python.org/ftp/python/3.9.5/python-3.9.5-amd64.exe.
I have a python execute files which runs every 5th minute and 35th minute of every hour. The script contains below code to run the content inside (itcmodel)
minutetorun = [5,35]
while True:
thisminute = datetime.now().minute
if (thisminute in minutetorun) and (thisminute != last_run):
itcmodel(loaded_model, configdata)
print("Windows service running.")
logging.info("Windows service running.")
last_run = thisminute
when the current minute matches minutetorun the itcmodel will run.
This entire script is converted to exe file. This exe file is triggered by NSSM.
Now my problem is, the service is stopping suddenly after working fine for an entire day or so. I have attached the log file below:
As you can see, the code was running fine until 00:05. Then it stopped giving output. After I restarted the service manually, it started working fine.
What might be the reason for this behavior? if the network was off, I will get the log to this file.
I checked the event viewer, it showed no action of system shut down or sleep.
What might be the reason for this behavior?
So I have a server which runs on windows system, and I need to schedule a daily task with windows task scheduler for a batch file which executes a python script which contains selenium operation (I am using a chrome driver), the expected outcome is to automatically download a file from online and then unzipped it to another folder. i.e. Program download the file and stored it in C:\download then unzipped it to E:\Data.
Everything is working fine if I set "Run only when user is logged on" in windows scheduler or I just ran the program manually. However, when I set "Run whether user is logged on or not", I won't see any file gets downloaded, so I suspect in order to let it work, one must log on to get webdriver interacts with website? I have refer to this site and seems like it is the case though the author is using edge (https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7290550/). Can anyone help to confirm this or can someone offer me a solution?
Moreover, if I manually leave the zipped file in i.e. C:\download, when the task starts with "Run whether user is logged on or not", I can see the file gets unzipped in the destination folder i.e. E:\Data which means part of my script (the extract part) is executed. So I am positive the task started successfully, but there's just no interaction in between selenium and website. Thanks.
P.S. My script does not contain any mapped driver, so this solution doesn't apply (https://social.technet.microsoft.com/Forums/windows/en-US/c03d6691-b058-4f8d-961c-e8eba25bbaed/task-scheduler-problem-run-whether-user-is-logged-on-or-not?forum=w7itprogeneral), and neither does this help (Task scheduler cannot open batch file when set to run whether user is logged on or not).
I have a program which is running on daily basis. I would like to have a log created for each run of it. Here is the snip of code responsible for logging:
logging.basicConfig(filename = 'log.txt', level = logging.DEBUG, format = '%(asctime)s - %(levelname)s - %(message)s')
logging.debug('Start of program') # example logging
Everything runs perfectly fine as long as I launch it by .py file or .bat file (mouse clicking in file explorer). Unfortunately, when I put it on the schedule the program runs fine but log file doesn't get created.
I have tried multiple scheduler settings but the problem seems to be located in the code of the script.
Thanks for feedback!
I wanted to add the solution as an answer too, since this seems to be a somewhat common problem which has caused me some grief in the past.
When executing something from the Windows Task Scheduler the working directory of that process is not by default the path of the executable but some other directory. E.g. when your Scheduler action calls C:\sample.py this is not executed in C:\ but C:\Windows\system32 (in my case).
You can verify this by adding a Scheduled Task calling a simple Python/Batch script which saves the active working directory to some file, such as this
import os
with open("C:\\cwd.txt", "w") as fh:
fh.write(os.getcwd())
or this
echo %cd% >> C:\cwd.txt
This messes with any relative paths your program may contain and, I suspect, it has some more subtle issues that I have not been able to pin down yet.
Problems can be avoided by explicitly setting the optional 'Start in' path for your action: