3年前写的了,现在看起来不那么完美,先保存一下。
# -*- coding: UTF-8 -*-
import os
from crontab import CronTab
'''
查看当前定时任务
'''
def cmd():
command = 'crontab -l' #查看当前的任务
output = os.popen(command).read()
return output #返回结果
'''
创建定时任务
'''
def time_around():
f=cmd()#调用查看命令
if f.find("ite_jsc") >= 0:
print("It's have a same crontab") #已经存在,不继续创建
else:
my_cron = CronTab(user=True) #True是当前用户
job = my_cron.new(command='sudo python3.6 /home//node_point_time/input_interface_all/ite_jsc.py')#因为是任务中的命令可显示sudo
job.minute.every(1) #每分钟执行一次,如果物理机重启后仍继续执行
my_cron.write() #写入任务
rs=cmd() #调用查看命令
if rs.find("ite_jsc") >= 0:
print("Time around create success") #创建成功
else:
print("Time around create false") #异常抛出
time_around()