Andereugh
Andereugh

Reputation: 1

python script runs in shared hosting ssh, but not in cron job

I have a godaddy shared hosting account, my goal was to use it to have a python script run as a cron job. I got it all working on my localhost, but when uploading to the godaddy server, I immediately found that the modules that my script imported were not available on the server and that it would be tricky to install them. Luckily I found this great tutorial: https://www.youtube.com/watch?v=pf2HS9gGI7k and following this I was able to install the modules successfully by commanding in SSH to change the python path, using:

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

Then I installed the modules by commanding in SSH:

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

I then successfully ran my pythonscript by commanding in SSH:

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

and it recognized the module and ran the script. But when I try to run it as a cronjob, I set the cronjob command to:

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

but the script doesn't work and has some problem with the python modules. So I guess that there is something I'm not doing correctly in this cronjob command, probably having to do with the python path. I would be very relieved if anyone had any suggestion about how to change this command or what else I should do.

The very long error I then get is:

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__'

I don't know if this could be affecting things, but when in SSH if I command:

cd ~
cd lib
ls

I get:

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

and all of my modules seems to be installed inside the above 'python/' folder in my frenzied attempts to figure out how to do this python script cronjob project I might have installed these python2.6/ python2.7/ folders before, which I think contain python2.6 and python2.7

as a side question, as a novice programmer with limited knowledge of Linux and ssh commands who wants to achieve this idea of running a python script on a web host through a cron job, having never done that before, what would have been the correct way to approach this, in contrast to what I did which was just start and try to figure out as I go along by google searching? I guess I am asking what would be the most efficient way to learn all that I needed to know about cronjobs on shared hosting and this wrinkle about having to install modules in this funny way. It would be great if godaddy could provide a brief overview of this.

Upvotes: 0

Views: 463

Answers (1)

Dmitry Orlov
Dmitry Orlov

Reputation: 473

What user does the cron job run under? Does that user have a $HOME variable in his environment? Probably not.

The correct way to approach this, IMHO, would be to

  1. familiarize yourself with virtualenv

  2. create a separate script sourceing the virtualenv and launching the script

  3. run that script as a cron job under your user (thisismygodaddyusername).

It's all very easy once you stop learning programming from YouTube and start reading books :)

Upvotes: 1

Related Questions