怎么在Python中使用crontab模块清除定时任务
本篇文章给大家分享的是有关怎么在Python中使用crontab模块清除定时任务,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
centos7下安装Python的pip
root用户使用yum install -y python-pip
时会报如下错误:
No package python-pip availableError:Nothing to do
解决方法如下:
首先安装epel扩展源:
yum-yinstallepel-release
更新完成之后,就可安装pip:
yum-yinstallpython-pip
安装完成之后清除cache:
yumcleanall
这是在root用户时使用的命令,当前用户如果不具有root权限,加上sudo。
在其他Linux类似centos衍生的发行版也可以用此方法解决。
安装python定时任务模块:
pipinstallpython-crontab
安装成功:可成功import
该模块
[root@centos7mnt]#python Python2.7.5(default,Jul132018,13:06:57) [GCC4.8.520150623(RedHat4.8.5-28)]onlinux2 Type"help","copyright","credits"or"license"formoreinformation. >>>importcrontab >>>
封装一个类,用来新增和清除定时任务:
#coding=utf-8 fromcrontabimportCronTab classCrontab_Update(object): def__init__(self): #创建当前用户的crontab,当然也可以创建其他用户的,但得有足够权限 self.cron=CronTab(user=True) #self.cron=CronTab(user='website') defadd_crontab_job(self,cmmand_line,time_str,commont_name,user): #创建任务 job=self.cron.new(command=cmmand_line) #设置任务执行周期 job.setall(time_str) #给任务添加一个标识,给任务设置comment,这样就可以根据comment查询 job.set_comment(commont_name) #将crontab写入配置文件 #self.cron.write() self.cron.write_to_user(user=user)#指定用户,写入指定用户下的crontab任务 defdel_crontab_jobs(self,comment_name,user): #根据comment查询,当时返回值是一个生成器对象, #不能直接根据返回值判断任务是否存在, #如果只是判断任务是否存在,可直接遍历my_user_cron.crons #jobs=self.cron.find_comment(commont_name) #返回所有的定时任务,返回的是一个列表 #a=self.cron.crons #print'a=',a #print'len(a)=',len(a) #按comment清除定时任务 #self.cron.remove_all(comment=comment_name) #按comment清除多个定时任务,一次write即可 self.cron.remove_all(comment=comment_name) self.cron.remove_all(comment=comment_name+'=') #清除所有定时任务 #self.cron.remove_all() #写入配置文件 #self.cron.write() self.cron.write_to_user(user=user)#指定用户,删除指定用户下的crontab任务 if__name__=="__main__": print'start--------' cmmand_line="/usr/bin/python/mnt/print_time.py" time_str="*****" commont_name="Test_Crontab_Job" user="xue" #创建一个实例 crontab_update=Crontab_Update() #调用函数新增一个crontab任务 #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-------'
定时任务执行的python脚本如下:print_time.py
#coding=utf-8 importdatetime #datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S") withopen('/mnt/datetime_log.txt','a')asf: f.write(datetime.datetime.now().strftime("%Y-%m-%d%H:%M:%S")+"\n") f.close()
设置定时任务后:
下面可通过命令查看,是否创建成功:
crontab-l
结果如下:
清除定时任务后:
以上就是怎么在Python中使用crontab模块清除定时任务,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注恰卡编程网行业资讯频道。
推荐阅读
-
Python中怎么动态声明变量赋值
这篇文章将为大家详细讲解有关Python中怎么动态声明变量赋值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文...
-
python中变量的存储原理是什么
-
Python中怎么引用传递变量赋值
这篇文章将为大家详细讲解有关Python中怎么引用传递变量赋值,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文...
-
python中怎么获取程序执行文件路径
python中怎么获取程序执行文件路径,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的...
-
Python中如何获取文件系统的使用率
Python中如何获取文件系统的使用率,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴...
-
Python中怎么获取文件的创建和修改时间
这篇文章将为大家详细讲解有关Python中怎么获取文件的创建和修改时间,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读...
-
python中怎么获取依赖包
今天就跟大家聊聊有关python中怎么获取依赖包,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据...
-
python怎么实现批量文件加密功能
-
python中怎么实现threading线程同步
小编给大家分享一下python中怎么实现threading线程同步,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!...
-
python下thread模块创建线程的方法
本篇内容介绍了“python下thread模块创建线程的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来...