Use python crontab to set linux scheduled tasks and pythoncrontab

Source: Internet
Author: User
Tags echo date

Use python crontab to set linux scheduled tasks and pythoncrontab

If you are familiar with linux, you should know that you can use crontab to set scheduled tasks in linux. You can run the crontab-e command to compile the task. Of course, you can also directly write the configuration file setting task.

But sometimes we want to use scripts to automatically set it, such as when we deploy the application. If you have any needs, you have to find a solution. Otherwise, you will be confused in the procedural ape community (a group of self-satisfied ape ).

Next, go to the topic and start to set it as a Write File. Simply add a line to the configuration file. However, it is difficult to read and write files. For example, when setting a task, you must check whether the task already exists and set the corresponding task according to the input parameters. It is not appropriate to read and write files. So I thought of the "omnipotent" Big python.

Dangdang: the python-crontab module. Install directly

$ pip install python-crontab

The following script allows you to conveniently set the scheduled task.

From crontab import CronTab # create the crontab of the current user. Of course, you can also create other users, but you must have sufficient permissions for my_user_cron = CronTab (user = True) # create a job = my_user_cron.new (command = 'echo date> ~ /Time. log') # set the task execution cycle and run the job every two minutes. setall ('*/2 *****') # Of course, other user-friendly settings are also supported, and some jobs are briefly listed. 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) # At the same time, you can set comment for the task so that you can query by comment, which is very convenient for job. set_comment ("time log job") # Based on the comment query, the returned value is a generator object at that time. You cannot directly determine whether the task exists based on the returned value #. If you only determine whether the task exists, you can directly traverse my_user_cron.cronsiter = my_user_cron.find_comment ('time log job') # query by command and execution cycle is also supported, which is similar to that of # disable and enable of the task. The default value is enablejob. enable (False) job. enable () # Write crontab to the configuration file my_user_cron.write ()

Run the following command to check whether the instance is successfully created:

$ crontab -l

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

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.