python脚本在共享主机ssh中运行,但不在cron作业中运行

时间:2018-01-20 13:40:52

标签: python module cron shared-hosting pythonpath

我有一个godaddy共享主机帐户,我的目标是使用它来运行作为cron作业的python脚本。我把它全部工作在我的localhost上,但是当上传到godaddy服务器时,我立即发现我的脚本导入的模块在服务器上不可用,并且安装它们会很棘手。幸运的是,我发现了这个很棒的教程:https://www.youtube.com/watch?v=pf2HS9gGI7k并且在此之后我能够通过命令在SSH中更改python路径来成功安装模块,使用:

cd ~
PYTHONPATH=$PYTHONPATH:/lib/python/
PYTHONPATH=$PYTHONPATH:$HOME/lib/python/

然后我通过SSH命令安装模块:

cd ~
wget http://urladdressofthisparticularmodule.tar.gz
tar xvf thisparticularmodule.tar.gz
cd thisparticularmodulesfolderthatthelaststepmade/
python setup.py install --home=~

然后我通过SSH命令成功运行了我的pythonscript:

cd ~
python /home/thisismygodaddyusername/public_html/thisismyscript.py

它识别了模块并运行了脚本。 但是当我尝试将其作为cronjob运行时,我将cronjob命令设置为:

PYTHONPATH=$PYTHONPATH:$HOME/lib/python python /home/thisismygodaddyusername/public_html/thisismyscript.py

但脚本不起作用,并且python模块存在一些问题。所以我想在我的cronjob命令中有一些我没有正确做的事情,可能与python路径有关。如果有人对如何更改此命令或我应该做什么有任何建议,我会非常放心。

我得到的很长的错误是:

File "/home/thisismygodaddyusername/public_html/thisismyscript.py", line 3, in <module>
  import MySQLdb
File "/home/thisismygodaddyusername/lib/python/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/__init__.py", line 19, in <module>

File "/home/thisismygodaddyusername/lib/python/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py", line 7, in <module>
File "/home/thisismygodaddyusername/lib/python/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py", line 3, in __bootstrap__
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3147, in <module>
  @_call_aside
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3131, in _call_aside
  f(*args, **kwargs)
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 3160, in _initialize_master_working_set
  working_set = WorkingSet._build_master()
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 657, in _build_master
  ws = cls()
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 650, in __init__
  self.add_entry(entry)
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 706, in add_entry
  for dist in find_distributions(entry, True):
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1966, in find_eggs_in_zip
  if metadata.has_metadata('PKG-INFO'):
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1488, in has_metadata
  return self.egg_info and self._has(self._fn(self.egg_info, name))
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1840, in _has
  return zip_path in self.zipinfo or zip_path in self._index()
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1717, in zipinfo
  return self._zip_manifests.load(self.loader.archive)
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1677, in load
  manifest = self.build(path)
File "/home/thisismygodaddyusername/.local/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1650, in build
  with zipfile.ZipFile(path) as zfile:
AttributeError: ZipFile instance has no attribute '__exit__'

我不知道这是否会影响事情,但是当我在SSH中时,如果我命令:

cd ~
cd lib
ls

我明白了:

./  ../  __init__.py  python/  python2.6/  python2.7/

我的所有模块似乎都安装在上面的'python /'文件夹中 在疯狂的尝试弄清楚如何做这个python脚本cronjob项目我可能已经安装这些python2.6 / python2.7 /文件夹之前,我认为包含python2.6和python2.7

作为一个附带问题,作为一个对Linux和ssh命令知之甚少的新手程序员,他们希望通过cron作业实现在Web主机上运行python脚本的想法,以前从未这样做过,本来应该是什么正确的方法来解决这个问题,与我刚开始做的事情形成鲜明对比,并试图弄清楚当我搜索谷歌的时候?我想我想问什么是最有效的方式来学习所有我需要知道的关于共享主机上的cronjobs和这个有关必须以这种有趣的方式安装模块的皱纹。如果godaddy可以提供一个简要的概述,那将是很好的。

1 个答案:

答案 0 :(得分:1)

cron作业运行的用户是什么?该用户的环境中是否有$ HOME变量?可能不是。

接下来的正确方法是恕我直言,

  1. familiarize yourself with virtualenv

  2. 创建一个单独的脚本source使用virtualenv并启动脚本

  3. 在您的用户(thisismygodaddyusername)下将该脚本作为cron作业运行。

  4. 一旦您停止从YouTube学习编程并开始reading books:)

    ,这一切都非常容易