Task Scheduler Not Always Run Batch File

pandionva-1921 0 Reputation points
2024-04-09T17:47:46.9866667+00:00

I have set a task to run a .bat file which runs 2 Python files.  This works successfully when I run it manually, and I have set it to wake up the computer (a desktop plugged into a UPS) twice a day 12 hours apart and run. Normally it does wake up the computer around the scheduled time, give or take about 5 minutes, and run the task even at the login screen, which can take up to about 10 to 15 minutes. At the end of the .bat file, there is a hibernate command. Whether the computer hibernates by this or when I manually hibernate it by clicking on the Start Menu -> Hibernate, the task wakes the computer up at the next scheduled time even if it's like half a day later and the process repeats. But I notice after almost a week, the computer turns on around the task-scheduled time but there's no indication if the .bat file has run this time. I got this computer as brand new a few years ago and have kept it updated. There was no power outage as I'm pretty sure the UPS was still running on power rather than battery solely, and I didn't touch the computer during this process. This was only 1 of the few tasks I've created as I have about 3 or so tasks and they are not scheduled around the same time. I did see some other tasks set/executing around the same time that seemed like they were set by the system (e. g. MicrosoftEdgeUpdateTaskMachineUA, etc.).

When I checked the history logs for the task, I saw this from latest to oldest:

Task Scheduler successfully finished "{...}" instance of the "\task1" task for user "......".

Task Scheduler successfully completed task "\task1" , instance "{...}" , action "C:\WINDOWS\SYSTEM32\cmd.exe" with return code 0.

Normally when the task runs the .bat file successfully before hibernating the computer, these are the logs I may see instead from latest to oldest:

Task Scheduler launched action "C:\WINDOWS\SYSTEM32\cmd.exe" in instance "{...}" of task "\task1".

Task Scheduler started "{...}" instance of the "\task1" task for user "......".

Task Scheduler launch task "\task1" , instance "C:\WINDOWS\SYSTEM32\cmd.exe"  with process ID ....

Task Scheduler launched "{...}"  instance of task "\task1" due to a time trigger condition.

My settings for the task are as follows:

  • Run only when user is logged on
  • Run with highest privileges
  • 2 daily triggers enabled
  • Actions Tab

    - Start a program: task1.bat

    - Start in (optional): C:\Users...\Documents\folder1

  • Start the task only if the computer is on AC power
  • Stop if the computer switches to battery power
  • Wake the computer to run this task
  • Allow task to be run on demand
  • Run task as soon as possible after a scheduled start is missed
  • If the task fails, restart every: 5 minutes

    - Attempt to restart up to: 3 times

  • Stop the task if it runs longer than 4 hours
  • If the running task does not end when requested, force it to stop
  • If the task is already running, then the following rule applies: Stop the existing instance
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
4,767 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,206 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 31,656 Reputation points
    2024-04-10T00:47:58.22+00:00

    Well, I'm not sure what your question is. But I can offer 2 suggestions.

    First, configure your scheduled task to capture stdout and stderr and in your .bat file add echo statements that log the time of day so that you know exactly when the task starts and ends.

    See my answer here: https://learn.microsoft.com/en-us/answers/questions/1637770/how-to-fix-shorter-run-in-task-scheduler-in-window

    For another users question, I wrote a Powershell script to watch the state changes of a task. This script also monitors the time of day clock, so if your PC hibernates, it should detect that. As written you would need to logon and run it interactively. You could modify it to log to a file if that works better for you.

    https://learn.microsoft.com/en-us/archive/msdn-technet-forums/c0be6139-5198-46b2-9135-4e6dae49adaa

    That might give you some insight into what your machine is doing.