1

I have this script, had some issues with it before, but anyway, now it works properly.

import croniter
import datetime
import re

now = datetime.datetime.now()

def main():

    f = open("/etc/crontab","r")
    f1 = f.readlines()
    for x in f1:
        if not re.match('^[0-9*]', x):
            continue
        a = re.split(r'\s+', x)
        cron = croniter.croniter(' '.join(a[:5]), now)
        cron.get_next(datetime.datetime)
        print(x)

if __name__ == "__main__":
    main()

This prints the content of my crontab file like this:

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly

25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

It's okay, but I'm using croniter for a reason, I could achieve this result by simply doing:

    def main():

        f = open("/etc/crontab","r")
        f1 = f.readlines()
        for x in f1:
            print(x)

    if __name__ == "__main__":
        main()

What I'm looking for, is to print this in a human-like way, something like this answer

Any ideas?

NeoVe
  • 3,857
  • 8
  • 54
  • 134
  • 2
    I believe you just need to change `print(x)` to `print(cron.get_next(datetime.datetime)), it will print the next run time like `2018-12-02 15:17:00` – cody Dec 02 '18 at 19:43
  • Damn man, lol, that was it hahaha, thanks you can put this as anser if you fancy :), Thank You again! – NeoVe Dec 02 '18 at 19:44
  • 1
    Haha nah, I wouldn't feel right double-dipping on a single question. I've updated the code on the original question you marked as answered. It displays the next runtime and the corresponding command. – cody Dec 02 '18 at 19:48
  • Awesome mate, Thanks! – NeoVe Dec 02 '18 at 19:51

0 Answers0