【问题标题】:Environment Variables returning None when running Python script from Cron [duplicate]从 Cron 运行 Python 脚本时环境变量返回 None [重复]
【发布时间】:2021-03-04 11:24:40
【问题描述】:

我已经在 /etc/profile 和 ~/profile 中设置了我的环境变量。当我运行 echo $EVNAME 时,它会正确返回,但作为 Cron 作业运行会返回 None。

我在 Raspberry Pi 4 上运行它并使用 Python3 编写脚本。

对这个有帮助吗?

【问题讨论】:

    标签: python cron environment-variables


    【解决方案1】:

    配置文件的正确路径是/etc/profile。如果您打算将其设置为特定用户,则为 ~/.bashrc

    请看这里:https://linuxize.com/post/how-to-set-and-list-environment-variables-in-linux/#persistent-environment-variables

    【讨论】:

    • 我已经在 /etc/profile、~/profile、.bash_profile 和现在 ~/.bachrc 中设置了我的 EV。 Cron 的结果始终相同,无。为什么 Cron 看不到这些 EV?
    • 你能在控制台中获取带有echo $MY_VAR的环境吗?您是否运行了source 命令来“重新加载环境变量?cronjob 是直接调用 python 脚本还是通过 shell 脚本?@Benzine
    • 基于相关的答案,它现在可以工作了。我补充说。等/个人资料;在运行我的脚本之前,它现在可以工作了。感谢您的帮助。
    猜你喜欢
    • 2017-06-26
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2018-04-17
    • 2018-02-07
    • 2013-02-05
    • 1970-01-01
    相关资源
    最近更新 更多