Search code examples
pythoncommand-linescheduled-taskswindows-task-schedulerwindows-11

Python script is not running in Task Scheduler


So I'm trying to run a Python script that automatically collects Microsoft rewards points, that's specifically in the main.py from this GitHub repo in Task Scheduler.

In Task Scheduler I have it set to "Run whether user is logged in or not". with "Run with highest privileges" checked.

The trigger is "Daily" and I have it to "8/27/2023 at 8:00 AM".

(https://i.sstatic.net/7pc7p.png)

In the Actions tab I have one Action - "Start a program" that's set to "C:\Python311\python.exe" (without quotes) with "Add arguments (optional)" set to ""C:\Users\Kid\Downloads\Microsoft-Rewards-Farmer-master\Microsoft-Rewards-Farmer-master\main.py" and "Start in (optional)" set to "C:\Users\Kid\Downloads\Microsoft-Rewards-Farmer-master\Microsoft-Rewards-Farmer-master".

(https://i.sstatic.net/V3Qre.png)

In the settings tab I have "Allow task to run on demand" checked, "Stop the task if it runs longer than: 8 hours" checked, and "If the running task does not end when requested, force it to stop" checked.

(https://i.sstatic.net/EfYdQ.png)

in the History tab I see (in order from top to down):

Task Scheduler launched "{4d6aad46-5acf-4b7a-9f98-f6a5a501ceb7}" instance of task "\MyTasks\rewards" for user "Kid" .

Task Scheduler successfully completed task "\MyTasks\rewards" , instance "{4d6aad46-5acf-4b7a-9f98-f6a5a501ceb7}" , action "C:\Python311\python.exe" with return code 2147942401.

Task Scheduler successfully finished "{4d6aad46-5acf-4b7a-9f98-f6a5a501ceb7}" instance of the "\MyTasks\rewards" task for user "DESKTOP-U67UPAV\Kid".

All off those events happened within the same minute which means it didn't run correctly

When I have done all of that, I then right clicked the task and pressed run.

It says its running but there's legit nothing in the logs file of the repo.

Any Ideas?

(I have even tried to make a bat file but it didn't work)

cmd /k "cd C:\Users\Kid\Downloads\Microsoft-Rewards-Farmer-master\Microsoft-Rewards-Farmer-master && main.py"

Also this is my first Stack overflow post so I apologize if I did something wrong when I asked this question.

Also, I know there are a lot of people reporting the same problem, (which I have tried, and running the task.) I have but it seems like each issue is specific to that person.


Solution

  • Consider using PowerShell to run the python command and redirect all error or output to a user-created log file. The history tab of the Task Scheduler will not capture runtime errors of actual jobs, just the status of job itself. It appears your code is erring out quickly and so exits quickly which is technically considered a completed job. Before setting up the task, check your very call in a PowerShell terminal by running:

    C:\Python311\python.exe "C:\Users\Kid\Downloads\Microsoft-Rewards-Farmer-master\Microsoft-Rewards-Farmer-master\main.py"
    

    Additionally, the benefit of this PowerShell approach is you can call other commands either python or other executables, capture timings, and other useful automation management needs.

    Below assumes python.exe is recognized in your Path environment variable (default in installation), so no need for full path to executable.

    PowerShell (save below as a .ps1 script in same folder as python script)

    & {
        echo "`nExecution Start: $(Get-Date -format 'u')"
    
        echo "`nRunning main.py- $(Get-Date -format 'u')"
        python main.py
        
        echo "`nExecution End: $(Get-Date -format 'u')"
    
    } 3>&1 2>&1 > "automation_job_$(Get-Date -format 'yyyy-MM-dd-HH-ss').log"
    

    Windows Task Scheduler

    • Program Script: PowerShell.exe
    • Add Arguments: -ExecutionPolicy bypass -File automation_job.ps1
    • Start in: C:\Users\Kid\Downloads\Microsoft-Rewards-Farmer-master\Microsoft-Rewards-Farmer-master