Detailed use Python crontab to set up Linux timing Tasks _python

Source: Internet
Author: User
Tags echo date

A friend who is familiar with Linux should know that you can use Crontab to set timed tasks in Linux. You can write a task by crontab-e the command. Of course, you can also write configuration files directly to set up tasks.

But sometimes you want to automate the setup of scripts, such as when we deploy the application. There is a need to find a way to solve, or in the process of ape mixed (a group of amused ape).

Let's get down to business, and start by adding a single line to the configuration file by setting it as a write file. But read and write files inevitably a bit cumbersome, and then for example: Set up tasks to check whether the task already exists, according to the input parameters to set the corresponding tasks. It's not appropriate to read and write files. So think of "omnipotent" big python.

When when, today's protagonist plays: Python-crontab module. Install Direct

$ pip Install Python-crontab

Here's a quick script to set up a scheduled task.

From crontab import crontab

# Create crontab for the current user, and of course you can create other users, but have sufficient permissions

My_user_cron = crontab (user=true)

# Create task

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

# Set task execution cycle, execute once every two minutes

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

of course also support other more humane settings, simply enumerate some

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 (4, 2))

Job.setall (DateTime, 4, 2, 2)

# at the same time you can set comment for the task, This can be based on comment query, very convenient

job.set_comment ("Time Log Job")

# According to the comment query, then the return value is a generator object, can not directly based on the return value to determine whether the task # exists, If you just judge if the task exists, you can traverse my_user_cron.crons

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

It also supports searching according to command and execution cycle, basically similar, no longer enumerates

# task disable and enable, default enable

job.enable (False)

job.enable ()

# finally writes crontab to the configuration file

My_user_cron.write () 

The following can be viewed by command to create a success:

$ crontab-l

Very convenient, there are some features not fully introduced, you can refer to the official documents Https://pypi.python.org/pypi/python-crontab

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.