Crontab The Python script is not a successful solution

Source: Internet
Author: User
Tags python script

Crontab often call some programs, but many times it is unsuccessful to call Python, but the manual is ok, when looking at the log (/var/log/cron) , but the display is normal, this situation how to break it? In fact, there are so many places:


1) Easily do not use if __name__ = = "__main__";

Many executives are accustomed to let their people write scripts into modular, and then take a graceful integration, but crontab to this method does not seem very bright, because Python runs from if __name__ = = "__main__" to start execution, And the above of those class crontab recognition is very laborious, but can only recognize function, and originally crontab is placed is some watchdog script, so write as much as possible, format not too complex.


2) note in English and Chinese, this is a hidden pit point, some crontab logs show that the script executes successfully, but the result is redirected to a file found in fact there is a problem of coding error;


3) Write absolute path, if it is Yum installation, write/usr/bin/python, if it is a custom installation, write the full path, script file also write absolute path;


4) Some Python scripts need to involve the database, but sometimes crontab cannot find the specific database, this time need to reset the environment variables such as Wow_path before the Python script execution, such as

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/87/FA/wKiom1flE5-D8hDdAAAmC3PdRhE748.png "title=" 111. PNG "alt=" Wkiom1fle5-d8hddaaamc3pdrhe748.png "/>


5) If none of the above works, then add Source/etc/profile to the crontab, as follows:

Absolute path to source/etc/profile && python py script


This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1855941

Crontab The Python script is not a successful solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.