This article mainly introduces you to the relevant information about the self-starting and scheduled tasks of Python scripts under Linux. The article introduces it in great detail through sample code. It has certain reference learning value for everyone to learn or use Python. Friends who need it Let’s follow the editor to learn together.
Preface
Recently, a colleague asked a question about Python script self-starting and scheduled tasks, and found that many friends are not familiar with this topic. I am particularly familiar with it, so this article mainly introduces to you the relevant content about the self-starting and scheduled tasks of Python scripts under Linux. It is shared for your reference and study. Without further ado, let’s take a look at the detailed introduction:
1. Let Python run automatically when Linux boots up
Prepare the script auto.py
to use Edit the following files with root permissions
sudo vim /ect/rc.local
Edit the command to start the script on exit 0
##
/usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto.log
2. Let the Python script start regularly
Prepare the script auto.py for scheduled startupsudo vim /etc/crontab
2 * * * * root /usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto.log
3. Explanation of crontab writing
Basic format* * * * * user command 分 时 日 月 周 用户 命令
4. Examples
##1. Execute once every minute
* * * * * user command
* */2 * * * user command (/表示频率)
30 8 * * * user command
30,50 * * * * user command(,表示并列)
30 8 3-6 * * user command (-表示范围)
30 8 * * 1 user command (周的范围为0-7,0和7代表周日)
The above is the detailed content of Detailed examples of how to self-start and schedule tasks in Python scripts under Linux. For more information, please follow other related articles on the PHP Chinese website!