This article example describes Python using the crontab module to set and clear the timed task operation. Shared for your reference, as follows:
Install pip for Python under centos7
The root user uses theyum install -y python-pip
The following error is reported:
No package python-pip available
Error:Nothing to do
The solution is as follows:
First install the epel extension source:
yum -y install epel-release
Once the update is complete, pip can be installed:
yum -y install python-pip
Clear the cache after the installation is complete:
yum clean all
This is the command to use when you are root, plus sudo if the current user does not have root privileges.
This can also be used in other Linux-like centos-derived distributions.
Install the python timed task module:
pip install python-crontab
Installation success: can be successfulimport
module
[root@centos7 mnt]# python Python 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import crontab >>>
Wraps a class to add and clear timed tasks:
# coding=utf-8 from crontab import CronTab class Crontab_Update(object): def __init__(self): # Create the current user's crontab, but you can create other users' crontabs, but only if you have sufficient permissions. = CronTab(user=True) # = CronTab(user='website') def add_crontab_job(self, cmmand_line, time_str, commont_name, user): # Create tasks job = (command=cmmand_line) # :: Setting up task execution cycles (time_str) # Add an identifier to the task and set a comment to the task so that it can be queried based on the comment job.set_comment(commont_name) # Write crontab to configuration file # () .write_to_user(user=user) # Specify a user, write the crontab tasks under the specified user def del_crontab_jobs(self, comment_name, user): # Query based on comment, when the return value is a generator object. # Cannot determine whether a task exists directly from the return value. # If you're just determining whether a task exists, you can just traverse my_user_cron.crons # jobs = .find_comment(commont_name) # Returns a list of all the timed tasks returned # a = # print 'a = ', a # print 'len(a) = ', len(a) # Clear timed tasks by comment # .remove_all(comment=comment_name) # Clear multiple timed tasks by comment, one write at a time .remove_all(comment=comment_name) .remove_all(comment=comment_name+ ' =') # Clear all timed tasks # .remove_all() # Write configuration file # () .write_to_user(user=user) # Specify the user, delete the crontab tasks under the specified user. if __name__ == "__main__": print 'start --------' cmmand_line = "/usr/bin/python /mnt/print_time.py" time_str = "* * * * *" commont_name = "Test_Crontab_Job" user = "xue" # Create an instance crontab_update = Crontab_Update() # Call the function to add a new crontab task # print '&&&&&& add_crontab_job ' # crontab_update.add_crontab_job(cmmand_line, time_str, commont_name, user) print '&&&&&& del_crontab_jobs ' crontab_update.del_crontab_jobs(commont_name, user) print 'end -------'
The python script for timed task execution is as follows: print_time.py
# coding=utf-8 import datetime # ().strftime("%Y-%m-%d %H:%M:%S") with open('/mnt/datetime_log.txt', 'a') as f: (().strftime("%Y-%m-%d %H:%M:%S")+"\n") ()
After setting a timed task:
The following command can be used to see if it was created successfully:
crontab -l
The results are as follows:
After clearing the timed task:
There are still some features that are not fully described, you can refer to the official documents/pypi/python-crontab
Readers interested in more Python related content can check out this site's topic: theSummary of Python date and time manipulation techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.