简体   繁体   English

在cron作业(Python)中,-m标志代表什么以及如何使用?

[英]In cron jobs (Python), what does the -m flag stand for and how is it used?

I am trying to set up a cron job which is executing a python file of mine. 我试图建立一个正在执行我的python文件的cron作业。 The python file is using some manually installed modules. python文件正在使用一些手动安装的模块。 The cron job now throws an error, as it 'cannot find' the specified module (yes, I tested it: if executed manually the script does work & have access to the module). 现在,cron作业会引发错误,因为它“找不到”指定的模块(是的,我已经对其进行了测试:如果手动执行,脚本确实可以工作并且可以访问该模块)。

I did now recieve the cryptic info (from the hoster's support) to ' try adding the -m flag to the command, followed by the path to the module that it cannot find. 我现在确实收到了来自主机提供商支持的神秘信息,以尝试在命令中添加-m标志,然后添加它找不到的模块的路径。 ' Unfortunatelly I do not quite understand this advice. 不幸的是,我不太理解这个建议。

Assuming that my cron job command ( via Cpanel ) would out of the box be: 假设我的cron job命令( 通过Cpanel )开箱即用:

0 * * * * python /home/public_html/cgi-bin/cronrun.py

which works if the python script does not rely on external modules. 如果python脚本不依赖外部模块,则可以使用。

So my questions are: 所以我的问题是:

  • Is the -m flag appropriate? -m标志合适吗?
  • If so, how do I use it? 如果可以,该如何使用?
  • And what do I do, if there is more than just one additional module that the script needs? 如果脚本需要的模块不止一个,那么我该怎么办?

Thank you very much in advance! 提前非常感谢您!

Your cron job won't likely be running with the same environment that you have. 您的Cron作业不可能在与您相同的环境下运行。 To see this, first run env > [somepath_that_you_can_reach]. 要查看此信息,请先运行env> [somepath_that_you_can_reach]。 Then set up a cron to do the same thing in a shell script using a different path. 然后设置cron以使用不同的路径在shell脚本中执行相同的操作。 Compare the two. 比较两者。 You will need your PYTHONPATH to be the same for the cron job to work. 为了使cron作业正常运行,您需要将PYTHONPATH设置为相同。 If that is the problem, then in your python script: 如果这是问题所在,则在您的python脚本中:

import sys
sys.path.append('[the path part that you need for it to work]')

before your import statements. 在导入语句之前。

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

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