python-3.xubuntu-20.04windows-subsystem-for-linuxwindows-task-schedulerwindows-10-iot-enterprise

Run a WSL-2 Ubuntu script on Windows 10 using Task Scheduler


Attempting to run the script from the Windows command prompt using the above fails. It can't find the file, regardless of whether I try the Ubuntu path or the windows path.

C:\>C:\Windows\System32\wsl.exe
user@ubuntumachinename:/$ # This is OK. 

C:\>C:\Windows\System32\wsl.exe "python3"
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> #This is OK too

C:\>C:\Windows\System32\wsl.exe "python3 /mnt/c/Users/.../automatic_temp_to_spreadsheet.py"
/bin/bash: python3 /mnt/c/Users/.../automatic_temp_to_spreadsheet.py: No such file or directory

C:\>C:\Windows\System32\wsl.exe "C:\Users\...\automatic_temp_to_spreadsheet.py"
/bin/bash: C:\Users\...\automatic_temp_to_spreadsheet.py: command not found

Note the script works if I run it directly from an Ubuntu WSL terminal window.

user@ubuntumachinename:~$ python3 ./automatic_temp_to_spreadsheet.py
Opening /mnt/c/Users/.../Heat tracking.csv...OK (5)
How many pieces of wood burned today(int)?
(Etc...)

Solution

  • OK.

    The answer was to add a shebang to the start of the script and removing the "python3" at beginning.

    So at the first line of the python script...

    #!/usr/bin/env python3

    And then change the command to...

    C:\Windows\System32\wsl.exe -e "/mnt/c/Users/.../automatic_temp_to_spreadsheet.py"

    enter image description here