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复制代码
Install and start the service
安装:apt-get install cron 启动/停止/重启:service cron start/stop/restart 查询当前任务:crontab -l复制代码
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:
In addition, this prompt may appear under ubuntu:
This also means that it can be used normally
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复制代码
View other users’ cron jobs:
crontab –l –u username复制代码
Remove scheduled cron jobs:
crontab –r复制代码
First, pass the following commandAdd or update tasks in crontab
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:
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复制代码
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表示复制代码
0 2 * * * command复制代码
0 5,17 * * * command复制代码
*/10 * * * * command复制代码
0 17 * jan,may,aug sun command复制代码
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 复制代码
or outputting this paragraph to txt:
echo "Hello xiaoyi" >> /tmp/test.txt 复制代码
Or you need to execute a Python script:
python demo.py filepath复制代码
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.
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.
First, the Python script is required to complete the following functions:
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)复制代码
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
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]
Here is a small suggestion, all Fill in the absolute path for all paths
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
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!