[英]Cannot run a python script using crontab
I am struggling to run the a python script as a cron job. 我正在努力将python脚本作为cron作业运行。
I am logged in as root the permission for the python script is 我以root身份登录,对python脚本的权限为
-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. 我测试了pythong脚本,如果我执行“ ./k_collab_spark.2.py”,则可以正常工作。
on the crontab i have set the job as 在crontab上,我将工作设置为
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 . 一旦我添加2>&1,它就会给出错误回溯(最近一次调用最近):在导入请求中,文件“ /opt/lampp/htdocs/kabeer/SPARK/k_collab_spark.2.py”第2行ImportError:没有名为请求的模块但是如果我手动执行该服务,它将成功。 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 PATH = / usr / local / sbin:/ usr / local / bin:/ sbin:/ bin:/ usr / sbin:/ usr / bin导入请求ImportError:没有名为请求的模块
Any idea what i am missing.. Appreciate any help around this. 任何想法,我想念的..赞赏有关此的任何帮助。
python
explicitly before the script name? python
吗? 2>&1
, which redirects error messages to the log file as well. 2>&1
,它将错误消息也重定向到日志文件。 See this link for a detailed description In the shell, what does " 2>&1 " mean? root
runs different versions of python. root
也可能运行不同版本的python。 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 . 如果它成功执行,则会在python解释器中出现问题,因为当您安装了多个版本的Python时,/ usr / bin / env将确保使用的解释器-是环境的$ PATH中的第一个解释器,我猜没有请求lib 。
I used a shell script to call the python script. 我使用了一个shell脚本来调用python脚本。 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 导出PATH = / opt / anaconda3 / bin:$ PATH / opt / anaconda3 / bin / python /opt/lampp/htdocs/scriptme.py> /opt/lampp/htdocs/scriptme.log 2>&1
Add the following lines of code to your script and edit the crontab : 将以下代码行添加到脚本中,然后编辑crontab:
from distutils.sysconfig import get_python_lib
print(get_python_lib())
Now check the log in crontab, you will get some path 现在检查crontab中的日志,您将获得一些路径
e.g. "/usr/lib/python2.7/dist-packages"
cd(change directory) to the above path and ls(list directory) to check if package exists ; cd(更改目录)到上述路径, ls(列表目录)检查软件包是否存在; if not :
如果没有 :
sudo pip3 install requests -t . # dot indicates current directory
or else if you have a requirements.txt file then you could try: 否则,如果您有requirements.txt文件,则可以尝试:
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. 现在运行您的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.