Home  >  Article  >  Backend Development  >  Detailed examples of how to self-start and schedule tasks in Python scripts under Linux

Detailed examples of how to self-start and schedule tasks in Python scripts under Linux

黄舟
黄舟Original
2017-08-07 15:38:262202browse

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

Finally restart Linux and the script will run automatically and print the log.


2. Let the Python script start regularly

Prepare the script auto.py for scheduled startup


Edit the following file with root permissions



sudo vim /etc/crontab

Add the following command at the end of the file



2 * * * * root /usr/bin/python3.5 /home/edgar/auto.py > /home/edgar/auto.log

The above code means to execute the script every two minutes and print the log.

3. Explanation of crontab writing

Basic format



* * * * * user command
分 时 日 月 周 用户 命令

4. Examples
##1. Execute once every minute


* * * * * user command

2. Execute once every 2 hours


* */2 * * * user command (/表示频率)

3. Execute once every day at 8:30


30 8 * * * user command

4. Execute once every 30 and 50 minutes every hour


30,50 * * * * user command(,表示并列)

4. From the 3rd to the 6th of every month, 8: Execute once at 30


30 8 3-6 * * user command (-表示范围)

5, execute once every Monday at 8:30


##
30 8 * * 1 user command (周的范围为0-7,0和7代表周日)


Summarize

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn