Python在Linux创建定时任务

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值