4

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. 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.

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.

Doing which python I get /usr/bin/pyhton and running python from the command line I can import and use smtplib just fine.

In /var/log/syslog I get:

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?

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

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. So that should not be a problem right?

4

2 回答 2

6

crons 有很多陷阱......在没有 MTA 的情况下,以更简单的方式查看输出 - 尝试将其通过管道传输到文件:

*/1 * * * * /home/pi/ipsender.py > /home/pi/ipsender.log 2>&1

用 a 启动你的 python 文件print 'starting',看看 python 文件是否开始执行正常。

于 2012-11-27T22:50:04.643 回答
1

尝试保存脚本的 STDOUT 和 STDERR:

*/1 * * * * /home/pi/ipsender.py &> /tmp/ipsender.log
于 2012-11-27T22:50:38.463 回答