python - 使用crontab执行py脚本出错
怪我咯
怪我咯 2017-04-18 09:57:14
0
2
533

最近使用centos6.8的crontab定时执行一个py脚本没有得到实际单独运行的效果

代码https://github.com/ansionfor/...

crontab -e 设置

脚本单独运行效果

crontab 日志有记录每分钟运行了py脚本

crontab处于启动状态

脚本的作用是保存某网站的文章到我自己远程mysql数据库中,但我测试了几天,发现没有一篇文章是通过这个定时器保存的,都是我自己手动运行脚本保存的文章


但是奇怪的是,我在crontab中放另外一个发送邮件的py脚本,却可以正常运行

代码https://github.com/ansionfor/...

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all (2)
阿神

1. If the subject added the sentence #!/usr/local/bin/python2.7 to xxx.py, then your crontab scheduled task definition only needs to be:

0 0 * * * /x/xxx.py

Because the first line of the script specifies the interpreter and it is already an executable script (file), there is no need for python2.7 to execute the command.

2. If you don’t write the first line to specify the environment variable, I usually write it like this because there are multiple versions in my environment

0 0 * * * /usr/local/bin/python2.7 /x/xxx.py

To specify the path for python execution, it is recommended that you write like this

3. Why does crontab need to specify the python path?

The official document explains this:

Environment variables have been automatically set, such as the current SHELL, SHELL=/bin/bash, and the HOME directory of the crontab owner is read from the /etc/passwd file. In addition to the default environment variables, other environment variables need to be set by the user. In order to find python2.7, it is necessary to set PATH=/user/local/bin

4. About debugging

Generally, if cron is not executed, an error email will be sent, usually under /var/mail by default

    大家讲道理

    After several tests, we came to the conclusion that there is a problem with the get_dongqiudi.py script, so the new problem is,

    This script can be executed manually, but why does it not take effect in crontab?


    I just rebuilt a mkfile.py file and put it in crontab to execute it regularly every minute. It can be executed normally

    But once the code is changed to this, it cannot be run in crontab and can only be run manually

    The following code

    #!/usr/local/bin/python3 import time file_name = '文章id'+ str(time.time()) f = open('/home/'+file_name,'w') f.close()
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!