• <legend id='fZEmN'><style id='fZEmN'><dir id='fZEmN'><q id='fZEmN'></q></dir></style></legend>

    1. <tfoot id='fZEmN'></tfoot>
      1. <i id='fZEmN'><tr id='fZEmN'><dt id='fZEmN'><q id='fZEmN'><span id='fZEmN'><b id='fZEmN'><form id='fZEmN'><ins id='fZEmN'></ins><ul id='fZEmN'></ul><sub id='fZEmN'></sub></form><legend id='fZEmN'></legend><bdo id='fZEmN'><pre id='fZEmN'><center id='fZEmN'></center></pre></bdo></b><th id='fZEmN'></th></span></q></dt></tr></i><div id='fZEmN'><tfoot id='fZEmN'></tfoot><dl id='fZEmN'><fieldset id='fZEmN'></fieldset></dl></div>
      2. <small id='fZEmN'></small><noframes id='fZEmN'>

          <bdo id='fZEmN'></bdo><ul id='fZEmN'></ul>

        cron运行python脚本时的环境变量

        时间:2023-07-04

      3. <i id='QffqJ'><tr id='QffqJ'><dt id='QffqJ'><q id='QffqJ'><span id='QffqJ'><b id='QffqJ'><form id='QffqJ'><ins id='QffqJ'></ins><ul id='QffqJ'></ul><sub id='QffqJ'></sub></form><legend id='QffqJ'></legend><bdo id='QffqJ'><pre id='QffqJ'><center id='QffqJ'></center></pre></bdo></b><th id='QffqJ'></th></span></q></dt></tr></i><div id='QffqJ'><tfoot id='QffqJ'></tfoot><dl id='QffqJ'><fieldset id='QffqJ'></fieldset></dl></div>
          <tbody id='QffqJ'></tbody>
        • <bdo id='QffqJ'></bdo><ul id='QffqJ'></ul>

                • <tfoot id='QffqJ'></tfoot>

                  <legend id='QffqJ'><style id='QffqJ'><dir id='QffqJ'><q id='QffqJ'></q></dir></style></legend>

                  <small id='QffqJ'></small><noframes id='QffqJ'>

                  本文介绍了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脚本时的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Amazon Elastic Beanstalk (Python) 中设置环境变量 下一篇:在 Windows 中将 DJANGO_SETTINGS_MODULE 永久设置为环境变量

                  相关文章

                • <small id='f9JM1'></small><noframes id='f9JM1'>

                • <tfoot id='f9JM1'></tfoot>

                      <bdo id='f9JM1'></bdo><ul id='f9JM1'></ul>
                    <i id='f9JM1'><tr id='f9JM1'><dt id='f9JM1'><q id='f9JM1'><span id='f9JM1'><b id='f9JM1'><form id='f9JM1'><ins id='f9JM1'></ins><ul id='f9JM1'></ul><sub id='f9JM1'></sub></form><legend id='f9JM1'></legend><bdo id='f9JM1'><pre id='f9JM1'><center id='f9JM1'></center></pre></bdo></b><th id='f9JM1'></th></span></q></dt></tr></i><div id='f9JM1'><tfoot id='f9JM1'></tfoot><dl id='f9JM1'><fieldset id='f9JM1'></fieldset></dl></div>
                    <legend id='f9JM1'><style id='f9JM1'><dir id='f9JM1'><q id='f9JM1'></q></dir></style></legend>