简体   繁体   English

如何在 conda env 中安排一个 python 脚本每 n 分钟运行一次?

[英]How can I schedule a python script inside a conda env to run every n minutes?

I have a python script getData.py on my mac which I would like to run once every 10 minutes or so.我的 Mac 上有一个 python 脚本 getData.py,我想每 10 分钟左右运行一次。 I am aware of crontab and have tried it before using virtual envs.我知道 crontab 并在使用虚拟环境之前尝试过它。 However I need help on how this can be achieved within a conda env that I do my work with.但是,我需要关于如何在我使用的 conda 环境中实现这一点的帮助。

Thanks谢谢

I have a bash script that I run as if it were regular python whenever I need a specific conda environment.我有一个 bash 脚本,每当我需要特定的 conda 环境时,我都会像运行常规 python 一样运行它。

#!/usr/bin/env bash
source <conda-directory>/etc/profile.d/conda.sh
# Uncomment this line to include .profile for environment variables
# source ~/.profile

conda activate <name-of-env>
python "$@"

Save this to a file and run将其保存到文件并运行

chmod +x <filename>

to make it executable.使其可执行。

Now you can run this script instead of python.现在你可以运行这个脚本而不是 python。 I called my file conda_python , and saved it in my home directory.我调用了我的文件conda_python ,并将其保存在我的主目录中。 To run a python script with this environment do要在此环境中运行 python 脚本,请执行以下操作

~/conda_python script.py

This should also work from crontab .这也应该适用于crontab

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM