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.

2
  • 2
    pythong :-) Commented Mar 5, 2016 at 1: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 Commented Mar 5, 2016 at 1:21

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

5 Comments

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 <module> 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
It seems that sometimes crontab can have different environment, see stackoverflow.com/questions/2388087/…. If you manually set PATH or PYTHONPATH in crontab according to the link it should work.
WOuld invoking the python script from a pearl script be a good idea?
When you say you run it manually are you doing it as root or another user?
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')
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.