Set Linux timing tasks using Python crontab

Source: Internet
Author: User
Tags echo date

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

But sometimes you want to automate the setup of scripts, such as when our application is deployed. There is a need, of course, to find a solution, or in the program Ape-bound (a group of the ape).

The following goes to the point, and began to want to write in the form of a file, by appending a line directly in the configuration file. However, it is difficult to read and write files, such as: To set the task to check whether the task already exists, according to the input parameters to set the corresponding task. It is not appropriate to read and write files. So think of the "Magnum" of the Big Python.

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

$ pip Install Python-crontab

The following script will make it easy to set up scheduled tasks.

From crontab import crontab# create crontab for the current user, but you can also 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 the task execution cycle, execute job.setall every two minutes (' */2 * * * * ') # and of course support other more humane settings, Simply list 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 (2)) Job.setall (Date (4, 2)) Job.setall (DateTime (2000, 4, 2, 10 , 2) # at the same time can be set to the task comment, so that can be based on comment query, very convenient job.set_comment ("Time Log Job") # According to comment query, when the return value is a generator object, You cannot directly determine whether a task # exists based on the return value, and if you only judge whether the task exists, you can traverse My_user_cron.cronsiter = my_user_cron.find_comment (' Time Log job ') # Also support based on command and execution cycle lookup, basically similar, no longer enumerate # task disable and enable, default enablejob.enable (False) job.enable () # finally write crontab to the configuration file My_ User_cron.write ()

The following commands can be used to see if the creation was successful:

$ crontab-l

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

Here is the son of old thin family, if need to reprint please declare, I thank you for old thin.

 

Set Linux timing tasks using Python crontab

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.