In Linux, cron is used to make Python programs run persistently. cronpython

Source: Internet
Author: User

In Linux, cron is used to make Python programs run persistently. cronpython

Under normal circumstances, it is difficult for a python program to keep running without errors, even if it is compiled as a sustainable file.

Fortunately, many requests do not require 24 hours of uninterrupted operation, but can be run once every other time.

The built-in cron in Linux makes this possible for us:

Input

crontab -e

At this time, the configured task will be displayed. If you have not set the task before, it is empty.

Set the task you want to run

* * * * * your-task

The parameters here are described as follows:

 

After the task is set, the system automatically runs the task, but some children's shoes may encounter such errors:

No module named '****'

You directly input python on the terminal, and then import this module, but it is OK,

This problem has plagued me a lot of time. The last possible cause is that there are multiple python versions in the system.

The cron call is not exactly what you want, and the solution is simple.

Write the detailed python path.

* * * * * /root/anaconda3/bin/python /root/btc.py

Note that the path in the red text section is the actual path of the python version you need.

 

If you need to output the task log to the specified file, you can do this:

* * * * * /root/anaconda3/bin/python /root/btc.py >> /root/error.txt

 

Below are some examples of cron usage

* ***** Run every minute. 0 * XXX runs hourly. 0 0 *** XXX runs at every day. 0 9,18 *** XXX runs at 9 and 6 every day. 0 9-18 *** XXX runs every hour from 9 AM to 6 PM. 0 9-18 ** 1-5 XXX runs hourly from 9 AM to 6 PM from Monday to Friday. */10 * XXX runs every 10 minutes.

 

Cron has many application scenarios, such:

Regularly back up your files, regularly back up the database, and send it to your mailbox.

Basically, with your programming language, scheduled and scheduled tasks can be completed.

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.