python 查看Linux cront

2023-01-31 05:01:22 python 查看 cront

#encoding:utf-8

#!/usr/local/bin/python

from crontab import CronTab

import sys,re

from croniter import croniter

from datetime import datetime

import Socket

import fcntl

import struct


def get_ip_address(ifname):

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    return socket.inet_ntoa(fcntl.ioctl(

        s.fileno(),

        0x8915,  # SioCGIFADDR

        struct.pack('256s', ifname[:15])

    )[20:24])


if __name__=='__main__':

    cron=CronTab(user='user0')

    host=get_ip_address('eth0')

    html='<html><head><title>'+host+'</title></head><body>'

    HTML+='<style>th,td{border:solid 1px #eee;}</style>'

    HTML+='<table><tr><th>Task</th><th>Prev Time</th><th>Next Time</th></tr>'

    for job in cron:

        #print job.command

        schedule = job.schedule(date_from=datetime.now())

        exec_next_time = schedule.get_next()

        exec_prev_time= schedule.get_prev()

        #print (job.command).replace(r'>/dev/null 2>&1','')+" next exec time:"+str(exec_next_time)+" prev exec time:"+str(exec_prev_time)

        task=(job.command).replace(r'>/dev/null 2>&1','')

        next=str(exec_next_time)

        prev=str(exec_prev_time)

        HTML+='<tr><td>%s</td><td>%s</td><td>%s</td></tr>'%(task,prev,next)

    HTML+='</table><body></html>'

    print HTML


相关文章