【问题标题】:how to schedule a script created in VScode (WSL) - Python如何安排在 VScode (WSL) 中创建的脚本 - Python
【发布时间】:2021-12-11 09:54:42
【问题描述】:

我有一个脚本,我想安排它每 10 分钟运行一次。 此脚本在此文件夹中下载一个 csv 文件。 C:\Users\rsharma\scripts\cylance\external_devices

脚本名称是 external_devices.py,我在 Visual Studio Code (WSL) 中创建了它。 现在我知道我可以使用 Crontab 和 windows 调度程序来安排它,但似乎没有任何工作。

这是我的脚本路径 C:\Users\rsharma\scripts\cylance\External_devices.py

这些是我尝试过的 crontab 条目 */10 * * * * /mnt/c/Users/rsharma/scripts/cylance/External_devices.py */10 * * * * /usr/bin/python3 /mnt/c/Users/rsharma/scripts/cylance/External_devices.py

Windows 调度程序条目 - 程序 - C:\WINDOWS\System32\wsl.exe 参数 - /mnt/c/Users/rsharma/scripts/cylance/External_devices.py

【问题讨论】:

  • 您不应该使用 WSL crontab。这个问题假设您使用的是 Windows 和 VScode,但您在 OP 中没有明确说明。 WSL crontab 不能保证在 Hyper-V lxss 系统进程中运行,因此所有自动 Windows 作业都应在 Windows 调度程序中定义。可能最好重新编写有关如何在 Windows 调度程序中运行 Python 脚本的问题。
  • 嗨 Rob,我是编的新手。很抱歉造成混乱。我的 Windows 机器上没有安装 python,它在 WSL 上。我将改写我的问题,但对解决方案有任何帮助。我怀疑我在 Windows 调度程序中为脚本提供的路径是否正确?

标签: scheduled-tasks windows-subsystem-for-linux


【解决方案1】:

请将脚本移出“/mnt/c/Users....”目录并复制到C盘的另一个目录。 WSL 挂载点仅在 lxss Hyper-V WSL 启动时可用,因此除非 VSCode 正在运行,否则不会挂载。现在最好使用 WSL2 (Ubuntu 20.4) 版本,您可以从 Microsoft 商店下载,然后作为扩展安装到“VSCode Remote Extensions for WSL”中,它将安装到“/home/username”中,并且速度要快得多并且功能丰富,特别是如果您打算使用直接集成的 Docker。

将 Windows Python 安装到您选择的目录中,例如 - c:\Python。您显然需要安装 Unix 版本使用的任何库。会有一个二进制文件

c:\Python\Python3.x\python.exe 

这需要被编码到一个批处理 windows 命令文件 (xxx.cmd) 中。要运行脚本,请更改脚本保存的目录并运行 python 命令。

xxx.cmd 批处理文件的示例代码:

cd c:\scriptdirectory
c:\Python\Python3.x\python.exe External_devices.py

在命令行上运行它以确保它正常工作,然后在 Windows 调度程序中进行适当的调度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多