[英]Cron python script not executing
I have read a few posts on this but I could not find help in them. 我已经阅读了几篇文章,但我找不到帮助。
I have a python script that sends mail using smtplib. 我有一个使用smtplib发送邮件的python脚本。 It works when invoked from the command line.
它从命令行调用时有效。
I have #!/usr/bin/python
as the first row and can run it both using /home/pi/ipsender.py
and python /home/pi/ipsender.py
. 我将
#!/usr/bin/python
作为第一行,并且可以使用/home/pi/ipsender.py
和python /home/pi/ipsender.py
运行它。
My crontab is */1 * * * * /home/pi/ipsender.py
but I have also tried */1 * * * * python /home/pi/ipsender.py
and */1 * * * * /usr/bin/python /home/pi/ipsender.py
. 我的crontab是
*/1 * * * * /home/pi/ipsender.py
但我也试过*/1 * * * * python /home/pi/ipsender.py
和*/1 * * * * /usr/bin/python /home/pi/ipsender.py
。
Doing which python
I get /usr/bin/pyhton
and running python from the command line I can import and use smtplib just fine. 做
which python
我得到/usr/bin/pyhton
并/usr/bin/pyhton
运行python我可以导入并使用smtplib就好了。
In /var/log/syslog
I get: 在
/var/log/syslog
我得到:
Nov 27 22:57:01 raspberrypi /USR/SBIN/CRON[3764]: (pi) CMD (python /home/pi/ipsender.py)
Nov 27 22:57:01 raspberrypi /USR/SBIN/CRON[3763]: (CRON) info (No MTA installed, discarding output)
And I'm guessing the No MTA... is just about Cron not sending emails about what it's doing, or isn't it? 我猜测没有MTA ......只是关于Cron没有发送关于它正在做什么的电子邮件,或者不是吗?
How do I run this script. 我该如何运行这个脚本。
[Edit] [编辑]
The permission of the script is 脚本的权限是
-rwxr-xr-x 1 pi pi 551 Nov 27 22:37 ipsender.py
[Edit2] Using the tip from D Read I'm getting the following log [Edit2]使用来自D Read的提示我得到以下日志
starting
Traceback (most recent call last):
File "/home/pi/ipsender.py", line 7, in <module>
ifconfig_output = sp.check_output(["ifconfig", "wwan0"])
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Suggesting to me that there is something with the path. 建议我有路径的东西。 Although in
/etc/crontab
I have PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
and which ifconfig
gives /sbin/ifconfig
. 虽然在
/etc/crontab
我有PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
which ifconfig
给出/sbin/ifconfig
。 So that should not be a problem right? 那应该不是问题吧?
There are lots of gotchas with crons... In the absence of the MTA, see the output an easier way - try piping it to a file: 有许多带有crons的问题......如果没有MTA,请查看输出更简单的方法 - 尝试将其连接到文件:
*/1 * * * * /home/pi/ipsender.py > /home/pi/ipsender.log 2>&1
Start your python file off with a print 'starting'
to see if the python file starts to execute ok. 使用
print 'starting'
打开你的python文件,看看python文件是否开始执行ok。
尝试保存脚本的STDOUT和STDERR:
*/1 * * * * /home/pi/ipsender.py &> /tmp/ipsender.log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.