Home > Backend Development > Python Tutorial > Detailed explanation of using python crontab to set up Linux scheduled tasks

Detailed explanation of using python crontab to set up Linux scheduled tasks

高洛峰
Release: 2017-02-10 09:26:33
Original
1815 people have browsed it

Friends who are familiar with Linux should know that you can use crontab to set up scheduled tasks in Linux. You can write tasks through the command crontab -e. Of course, you can also write configuration files directly to set up tasks.

But sometimes we want to set it automatically through scripts, such as when our application is deployed. If there is a need, of course you have to find a way to solve it, otherwise you will end up in the world of programmers (a group of self-satisfied apes).

Now let’s get to the point. I want to set it up by writing a file. Just add a line directly to the configuration file. However, reading and writing files is inevitably a bit cumbersome. Another example is: when setting a task, you need to check whether the task already exists; set the corresponding task according to the input parameters, etc. Reading and writing files is inevitably inappropriate. So I thought of the "universal" big python.

Dangdangdang, today’s protagonist comes on stage: the python-crontab module. Install directly

$ pip install python-crontab
Copy after login

You can easily set up scheduled tasks on the script

from crontab import CronTab

# 创建当前用户的crontab,当然也可以创建其他用户的,但得有足够权限

my_user_cron = CronTab(user=True)

# 创建任务

job = my_user_cron.new(command='echo date >> ~/time.log')

# 设置任务执行周期,每两分钟执行一次

job.setall('*/2 * * * *')

# 当然还支持其他更人性化的设置方式,简单列举一些

job.minute.during(5,50).every(5)

job.hour.every(4)

job.day.on(4, 5, 6)

job.dow.on('SUN')

job.dow.on('SUN', 'FRI')

job.month.during('APR', 'NOV')

job.setall(time(10, 2))

job.setall(date(2000, 4, 2))

job.setall(datetime(2000, 4, 2, 10, 2))

# 同时可以给任务设置comment,这样就可以根据comment查询,很方便

job.set_comment("time log job")

# 根据comment查询,当时返回值是一个生成器对象,不能直接根据返回值判断任务是否#存在,如果只是判断任务是否存在,可直接遍历my_user_cron.crons

iter = my_user_cron.find_comment('time log job')

# 同时还支持根据command和执行周期查找,基本类似,不再列举

# 任务的disable和enable, 默认enable

job.enable(False)

job.enable()

# 最后将crontab写入配置文件

my_user_cron.write()
Copy after login

You can use the command below to check whether the creation is successful:

$ crontab -l
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study, and I also hope that everyone will support the php Chinese website.

For more detailed explanations on using python crontab to set up Linux scheduled tasks, please pay attention to the PHP Chinese website for related articles!

Related labels:
source:php.cn
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