0

I need to read the contents of the /etc/crontab file.

Right now I have this:

import croniter
import datetime

now = datetime.datetime.now()


def main():

    f = open("/etc/crontab","r")
    f1 = f.readlines()
    cron = croniter.croniter(f1, now)
    for x in f1:
        cron.get_next(datetime.datetime)
        print(x)

if __name__ == "__main__":
    main()

What I want is to print the next time a task will run, based on what's defined on my crontab file, I have followed this answer, however I need to actually read this from a file (crotab file that is) then print it to stdout.

Right now it throws me this:

Traceback (most recent call last):
File "cron.py", line 17, in <module>
main()
File "cron.py", line 11, in main
cron = croniter.croniter(f1, now)
File "/home/user/.virtualenvs/rest_tails2/lib/python3.6/site-packages/croniter/croniter.py", line 92, in __init__
self.expanded, self.nth_weekday_of_month = self.expand(expr_format)
File "/home/user/.virtualenvs/rest_tails2/lib/python3.6/site-packages/croniter/croniter.py", line 464, in expand
expressions = expr_format.split()
AttributeError: 'list' object has no attribute 'split'

Any ideas on this? I'm very new to croniter, there's also python-crontab but haven't used it yet.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
NeoVe
  • 3,857
  • 8
  • 54
  • 134

1 Answers1

1

croniter handles a single cron-expression. You should have it inside the loop, and apply it to each row individually:

for x in f1:
    cron = croniter.croniter(x, now) # Here!
    cron.get_next(datetime.datetime)
    print(x)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Thank You very much, sorry, just a question, now it throws me `croniter.croniter.CroniterBadCronError: Exactly 5 or 6 columns has to be specified for iteratorexpression.` – NeoVe Dec 02 '18 at 18:06