Environment Variables when python script run by cron(cron运行python脚本时的环境变量)
问题描述
我一直在查看其他堆栈溢出问题,但无法解决任何问题.我有一个使用环境变量的 python 脚本.该脚本在直接运行时完全按计划运行,但是我想暂时将其作为 cron 作业每分钟运行一次.
I have been looking at other stack overflow questions but couldn't get any to work. I have a python script which uses environment variables. This script works exactly as planned when run directly however, I would like to run it as a cron job every minute for the time being.
目前在我的 cron.d 目录中,我有一个名为 scrapers 的文件,其中包含:
Currently in my cron.d directory I have a file called scrapers containing:
* * * * * root /usr/bin/python3.5 /code/scraper.py
这会运行 python 脚本,但脚本失败,因为在脚本中我使用了两个环境变量.
This runs the python script but the script fails as in the script I use two environment variables.
我读到我应该将 SHELL=/bin/bash 添加到 cron 文件中,所以我这样做了,但这没有帮助.
I read I should add SHELL=/bin/bash to the cron file so I did but this didn't help.
SHELL=/bin/bash
* * * * * root /usr/bin/python3.5 /code/scraper.py
然后我读了
在 crontab 中,在您发出命令之前,添加 .$HOME/.profile.
In the crontab, before you command, add . $HOME/.profile.
SHELL=/bin/bash
* * * * * . $HOME/.profile; root /usr/bin/python3.5 /code/scraper.py
但这导致 cron 完全停止运行.将环境变量发送"到 cron 的最佳方式是什么?
but this caused the cron to stop running altogether. What is the best way of 'sending' the env variables to the cron?
推荐答案
我要做的不是执行整个 ~/.profile
而是移动必须在您的 cron
作业和具有配置文件的帐户,然后我会在 ~/.profile
和 cron 作业中获取这些.
Instead of executing the whole ~/.profile
what I'd do is move the variables that must be shared between your cron
jobs and the account that has the profile, then I'd source these both in ~/.profile
and in the cron job.
您在问题中显示的最后一次尝试格式不正确.用户 ID 应该紧跟在计划信息之后,但是您在用户 ID 之前添加了配置文件的来源,这肯定行不通.
The last attempt you show in the question is not properly formatted. The user id should be coming right after the scheduling information, but you've added the sourcing of the profile before the user id, which surely cannot work.
这是我在这里测试过的示例设置:
Here's an example setup that I've tested here:
*/1 * * * * someuser . /tmp/t10/setenv && /usr/bin/python /tmp/t10/test.py
出于测试目的,我已将其设置为每分钟执行一次.将 someuser
替换为有意义的内容.我使用的 /tmp/t10/setenv
脚本有这个:
I've set it to execute every minute for testing purposes. Replace someuser
with something that makes sense. The /tmp/t10/setenv
script I used had this:
export FOO=foovalue
export BAR=barvalue
/tmp/t10/test.py
文件有这个:
import os
print os.environ["FOO"], os.environ["BAR"]
我的 cron 将它运行的脚本的输出通过电子邮件发送给我.我收到了一封包含此输出的电子邮件:
My cron emails me the output of the scripts it runs. I got an email with this output:
foovalue barvalue
这篇关于cron运行python脚本时的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cron运行python脚本时的环境变量


基础教程推荐
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01