如何在 crontab 中使用 Anaconda 环境 运行 我的 Python 脚本?
How to run my Python script with Anaconda environment in crontab?
- 我想每天在特定的时间在特定的 anaconda 环境中使用 crontab 将任务安排到 运行 一个 python 文件。
- 我还有一个 python 脚本可以这样做。
- pythons 脚本 运行s 如果我只是在终端的 anaconda evoronment 中用
python h.py
执行它。 h.py 在主目录中
- 我正在使用 Ubuntu 20.04,我还没有刷新安装任何新的 cron 或 crontab
- 我已经尝试了以下命令来让它工作,但它们什么也没做(结果应该是一个文件夹,但它几乎没有被创建)
crontab -e
在 crontab 内:
#[long descriptional text]
...
53 13 * * * cd /home/ && /home/user/anaconda3/envs/rapids/bin/python h.py
这个 alos 什么都不做,没有错误信息
我也尝试过以下解决方案
32 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py
34 14 * * * cd /home/Documents && /home/anaconda3/envs/rapids/bin/python h.py 2>&1
44 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py >> ~/cron.log 2>&1
- 不适用于普通的 anaconda - https://unix.stackexchange.com/a/572951/400960
58 14 * * * /home/Documents && /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
59 14 * * * /home/Documents && /home/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
58 14 * * * /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py
10 15 * * * /home/anaconda3/envs/rapids/bin/python home/Documents/h.py
- 运行 这用于分析目的,没有结果(没有创建文件,终端没有打印输出)
36 15 * * * /home/anaconda3/envs/rapids/bin/python -c "import sys; sys.stdout.write('out\n'); sys.stderr.write('err\n')" >> /home/so_test.log 2>&1
我也阅读了以下解决方案,但没有任何效果
- 1 thinga 可能会解决这个问题,我不确定我是否需要为 crontab 安装守护进程,就像这里推荐的那样:https://askubuntu.com/a/1048365/984498 但我无法找到 [=91= 的 cronie 安装]
- https://unix.stackexchange.com/questions/300128/how-can-i-run-a-python-script-using-anaconda-from-the-command-line
- https://unix.stackexchange.com/questions/574085/crontab-service-file-not-found-despite-installed-and-configured-crontab
- https://askubuntu.com/questions/1021622/crontab-doesnt-run-python-script
- Execute Python script via crontab
如果Python文件只需要python(不需要其他库)
56 16 * * * /home/MY_ACTUAL_USERNAME/anaconda3/envs/rapids/bin/python /home/MY_ACTUAL_USERNAME/Documents/h.py
如果Python文件需要anaconda环境中的其他python库:
- 创建 SHELL 脚本
nano my_sehell_file_name.sh
- 示例文件中应包含的内容
#!/bin/bash
#conda activate rapids WRONG
source ~/anaconda3/bin/activate MY_ANACONDA_ENVIRONMENT_NAME #correct
#python Documents/my_python_file_name.py WRONG SEPARATLY GO TO FOLER WHTAN EXECUTE EITH python
cd ~/Documents/folder_where_python_file_is/ #correct
python my_python_file_name.py #correct
conda deactivate
- 通过
启动 corntab
corntab -e
- 例如您可以写入此 corntab 文件末尾的内容
43 21 * * * /home/MY_ACTUAL_USERNAME/my_sehell_file_name.sh
- 我想每天在特定的时间在特定的 anaconda 环境中使用 crontab 将任务安排到 运行 一个 python 文件。
- 我还有一个 python 脚本可以这样做。
- pythons 脚本 运行s 如果我只是在终端的 anaconda evoronment 中用
python h.py
执行它。 h.py 在主目录中 - 我正在使用 Ubuntu 20.04,我还没有刷新安装任何新的 cron 或 crontab
- 我已经尝试了以下命令来让它工作,但它们什么也没做(结果应该是一个文件夹,但它几乎没有被创建)
crontab -e
在 crontab 内:
#[long descriptional text]
...
53 13 * * * cd /home/ && /home/user/anaconda3/envs/rapids/bin/python h.py
这个 alos 什么都不做,没有错误信息
我也尝试过以下解决方案
32 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py
34 14 * * * cd /home/Documents && /home/anaconda3/envs/rapids/bin/python h.py 2>&1
44 14 * * * cd /home/Documents && /home/user/anaconda3/envs/rapids/bin/python h.py >> ~/cron.log 2>&1
- 不适用于普通的 anaconda - https://unix.stackexchange.com/a/572951/400960
58 14 * * * /home/Documents && /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
59 14 * * * /home/Documents && /home/anaconda3/envs/rapids/bin/python home/Documents/h.py >> ~/cron.log 2>&1
58 14 * * * /home/user/anaconda3/envs/rapids/bin/python home/Documents/h.py
10 15 * * * /home/anaconda3/envs/rapids/bin/python home/Documents/h.py
- 运行 这用于分析目的,没有结果(没有创建文件,终端没有打印输出)
36 15 * * * /home/anaconda3/envs/rapids/bin/python -c "import sys; sys.stdout.write('out\n'); sys.stderr.write('err\n')" >> /home/so_test.log 2>&1
我也阅读了以下解决方案,但没有任何效果
- 1 thinga 可能会解决这个问题,我不确定我是否需要为 crontab 安装守护进程,就像这里推荐的那样:https://askubuntu.com/a/1048365/984498 但我无法找到 [=91= 的 cronie 安装]
- https://unix.stackexchange.com/questions/300128/how-can-i-run-a-python-script-using-anaconda-from-the-command-line
- https://unix.stackexchange.com/questions/574085/crontab-service-file-not-found-despite-installed-and-configured-crontab
- https://askubuntu.com/questions/1021622/crontab-doesnt-run-python-script
- Execute Python script via crontab
如果Python文件只需要python(不需要其他库)
56 16 * * * /home/MY_ACTUAL_USERNAME/anaconda3/envs/rapids/bin/python /home/MY_ACTUAL_USERNAME/Documents/h.py
如果Python文件需要anaconda环境中的其他python库:
- 创建 SHELL 脚本
nano my_sehell_file_name.sh
- 示例文件中应包含的内容
#!/bin/bash
#conda activate rapids WRONG
source ~/anaconda3/bin/activate MY_ANACONDA_ENVIRONMENT_NAME #correct
#python Documents/my_python_file_name.py WRONG SEPARATLY GO TO FOLER WHTAN EXECUTE EITH python
cd ~/Documents/folder_where_python_file_is/ #correct
python my_python_file_name.py #correct
conda deactivate
- 通过 启动 corntab
corntab -e
- 例如您可以写入此 corntab 文件末尾的内容
43 21 * * * /home/MY_ACTUAL_USERNAME/my_sehell_file_name.sh