2

I am struggling to run the a python script as a cron job.

I am logged in as root the permission for the python script is

-rwxr-xr-x 1 root root 2374 Mar  1 22:49 k_collab_spark.2.py

I am starting the script with

#!/usr/bin/env python

I tested the pythong script if i do "./k_collab_spark.2.py` this work fine.

on the crontab i have set the job as

15 12 * * * /opt/lampp/htdocs/testme/SPARK/k_collab_spark.2.py >> /var/log/kspark.log

I do not see any message on the log file

Once i adde 2>&1 it gives an error Traceback (most recent call last): File "/opt/lampp/htdocs/kabeer/SPARK/k_collab_spark.2.py", line 2, in import requests ImportError: No module named requests but if i execute the service manually it is successful . WHen i run it manually it works fine

Tried defining the path but still the same issue

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin import requests ImportError: No module named requests

Any idea what i am missing.. Appreciate any help around this.

NKB
  • 175
  • 2
  • 12
  • 2
    [pythong](http://www.amazon.com/Hustler-Fundies-NU17-Monster-Python/dp/B009T6GABE) :-) – Martin Tournoij Mar 05 '16 at 01:18
  • 1
    Do you see anything in the logfiles? You didn't mention which Linux you're using, but this is usually `/var/log/cron`. It's also possible the user running this has a mail with an error, so be sure to check that (if there's no alias in `/etc/aliases`, log in and run `mail`, or check `/var/mail/$user` – Martin Tournoij Mar 05 '16 at 01:21

4 Answers4

1

Try to run script with another first line:

#!/usr/bin/python

If it's executes successfully the problem in python interpreter, because when you have several versions of Python installed, /usr/bin/env will ensure the interpreter used - is the first one on your environment's $PATH, which i guess has no requests lib.

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82
1

Add the following lines of code to your script and edit the crontab :

from distutils.sysconfig import get_python_lib
print(get_python_lib())

Now check the log in crontab, you will get some path

e.g. "/usr/lib/python2.7/dist-packages"

cd(change directory) to the above path and ls(list directory) to check if package exists ; if not :

sudo pip3 install requests -t . # dot indicates current directory 

or else if you have a requirements.txt file then you could try:

sudo pip3 install -r requirements.txt -t "/usr/lib/python2.7/dist-packages" 
#try this from the directory where  "requirements.txt" file exists

Now run your scripts.

DeWil
  • 362
  • 3
  • 10
0
  1. Can you add python explicitly before the script name?
  2. At the end of the crontab line, add 2>&1, which redirects error messages to the log file as well. See this link for a detailed description In the shell, what does " 2>&1 " mean?
  3. There is also a possibility that your current user and root runs different versions of python.
Community
  • 1
  • 1
Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • Once i adde 2>&1 it gives an error Traceback (most recent call last): File "/opt/lampp/htdocs/kabeer/SPARK/k_collab_spark.2.py", line 2, in import requests ImportError: No module named requests but if i execute the service manually it is successful . WHen i run it manually it works fine – NKB Mar 05 '16 at 04:21
  • It seems that sometimes `crontab` can have different environment, see http://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths. If you manually set PATH or PYTHONPATH in crontab according to the link it should work. – Xiongbing Jin Mar 05 '16 at 04:37
  • WOuld invoking the python script from a pearl script be a good idea? – NKB Mar 05 '16 at 07:51
  • When you say you run it manually are you doing it as root or another user? – Gideon Maina Mar 05 '16 at 09:40
  • Can you add this to the beginning of the python code and see if it works? `import sys`, `sys.path.append('/path_to_your_lib')` – Xiongbing Jin Mar 05 '16 at 16:01
0

I used a shell script to call the python script. THe anaconda on the box was causing the trouble

export PATH=/opt/anaconda3/bin:$PATH /opt/anaconda3/bin/python /opt/lampp/htdocs/scriptme.py >/opt/lampp/htdocs/scriptme.log 2>&1

NKB
  • 175
  • 2
  • 12