Python scheduled tasks, a method to achieve automation

coldplay.xixi
Release: 2020-11-16 17:12:06
forward
2637 people have browsed it

Python Tutorial column introduces methods to achieve automation.

Python scheduled tasks, a method to achieve automation

1. Install cron

Basically all Linux distributions have the cron tool pre-installed by default.

Even if cron is not pre-installed, it is very simple. You can install it manually by executing a few simple commands

# 检查是否已经预装了cron
service cron status复制代码
Copy after login

Install and start the service

安装:apt-get install cron
启动/停止/重启:service cron start/stop/restart
查询当前任务:crontab -l复制代码
Copy after login

2. Installation check

After the installation is completed, check whether the installation is successful. Also use the status command to check

If the following prompt appears, the installation is successful:

Python scheduled tasks, a method to achieve automation

In addition, this prompt may appear under ubuntu:

Python scheduled tasks, a method to achieve automation

This also means that it can be used normally

3. cron usage

There are several simple usages of cron that you can learn about, and a case will be used to introduce how to use them in detail later

First, list the cron jobs planned by the current user:

crontab -l复制代码
Copy after login

View other users’ cron jobs:

crontab –l –u username复制代码
Copy after login

Remove scheduled cron jobs:

crontab –r复制代码
Copy after login

4. Schedule crontab plan

First, pass the following commandAdd or update tasks in crontab

Python scheduled tasks, a method to achieve automation

You will be asked to choose an editor when you first enter. This choice is based on your own habits.

After selecting, you will enter an interface like this:

Python scheduled tasks, a method to achieve automation

Students who have used vim should be familiar with this interface. Similar operations: Press A to start editing, press ESC to enter wq to save and exit

The key point is the bottom paragraph:

m h dom mon dow commmand复制代码
Copy after login

This is actually a use of crontab scheduling jobs Introduction, can be used to set up scheduled tasks.

The specific syntax is as follows:

m h dom mon dow command
* * * * * command
- - - - - -
| | | | | |
| | | | | --- 预执行的命令
| | | | ----- 表示星期0~7(其中星期天可以用0或7表示)
| | | ------- 表示月份1~12
| | --------- 表示日期1~31
| ----------- 表示小时1~23(0表示0点)
------------- 表示分钟1~59 每分钟用*或者 */1表示复制代码
Copy after login

Give a few simple application cases:

  • Execute tasks at 02:00 every day
0 2 * * * command复制代码
Copy after login
  • Perform tasks at 5:00 and 17:00 every day
0 5,17 * * * command复制代码
Copy after login
  • Perform tasks every 10 minutes
*/10 * * * * command复制代码
Copy after login
  • Execute tasks at 17:00 on Sundays in certain months
0 17 * jan,may,aug sun command复制代码
Copy after login

These are the most commonly used ones. For more use cases, you can refer to this link: http://linux.51yip .com/search/crontab

The command in the above case indicates the specific task you need to perform, such as printing a paragraph:

echo "Hello xiaoyi" >> /tmp/test.txt 
复制代码
Copy after login
Copy after login

or outputting this paragraph to txt:

echo "Hello xiaoyi" >> /tmp/test.txt 
复制代码
Copy after login
Copy after login

Or you need to execute a Python script:

python demo.py filepath复制代码
Copy after login

The filepath behind represents the input parameter args, which may be used by some students. For example, in the following case, you need to enter the file download path.

5. Practical combat

After you have figured out the above, you can start today’s highlight.

First of all, we need to download the latest task data from the ftp server every day, download the data to the local computer and conduct data summary statistics through Python, and finally store the results in the database. If a certain link occurs during the If there is a problem, an alert email will be sent.

① Python script

First, the Python script is required to complete the following functions:

  • Get the latest data date from the database
  • From ftp Download the latest data to the local
  • Conduct summary statistics on the latest local data
  • Statistical results are stored in the database summary
  • E-mail notification

The above The rough pseudo code of the process is as follows:

if __name__ == '__main__':    """获取最新数据日期"""
    latest_date = get_max_date()    # 以最新日期为名创建文件夹
    download_dir = os.path.join(sys.argv[1], latest_date)    if not os.path.exists(download_dir):
        os.makedirs(download_dir)        
    """从ftp中下载最新数据"""
    download_file(latest_date, download_dir)    """处理最新数据并保存"""
    process_data(latest_date, download_dir)复制代码
Copy after login

Email monitoring can add a try catch exception capture, and when an exception occurs, an email will be sent

Python editor I have written and sent the email content before. You can refer to the following: Python email sending

②Writing cron task

Open crontab, edit the following content to the last line, save and exit

crontab will automatically update the task list in real time. If you are not sure, you can also restart the cron service through the restart command [refer to the beginning of the article]

Python scheduled tasks, a method to achieve automation

Here is a small suggestion, all Fill in the absolute path for all paths

③ Effect monitoring

If there is no problem with the Python code, the task will be executed regularly.

It is recommended that you run your own command in the console alone, and then write it to the cron task list when there are no problems.

The final screenshot of Xiaoyi's scheduled task operation is as follows:

The bottom is the ftp file download, and the top is the data summary statistics

Python scheduled tasks, a method to achieve automation

Related free learning recommendations: python tutorial(Video)

The above is the detailed content of Python scheduled tasks, a method to achieve automation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.im
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template