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.