mcobbley has asked for the wisdom of the Perl Monks concerning the following question:

I am extremely new to PERL. I have been tasked with finding a way to parser out the lines of a crontab and then monitor those lings have been preformed. As I have searched the web I found information on how to build a crontab using PERL but nothing on how to monitor it. Anyone know where I can find a how to on this.

Replies are listed 'Best First'.
Re: Parsing and monitoring Crontabs
by jethro (Monsignor) on Jul 12, 2010 at 11:41 UTC

    I don't know of any monitoring interface to cron. So probably the only possibilites are

    1) Monitor the logfile where cron posts its execution infos, maybe parse it with something like Regexp::Log

    2) if you have the power to change crontab entries, you might add a wrapper script to every line that records the execution statistics to a file. Trivially this could be just adding 'time' before every command so that the user gets an email for every execution. The emails then would have to be captured and analyzed, which is the tricky part (A real wrapper script would be simpler overall in most cases)

      like toolic said.

      there is a read method/ write method in Config::Crontab which allows you to read the contents, and if needed even delete a crontab entry.

      Its looks quite formidable at first, but the examples there give a clearer picture.

Re: Parsing and monitoring Crontabs
by JavaFan (Canon) on Jul 12, 2010 at 12:08 UTC
    Details will vary from OS to OS, but usually cron will use the syslog service to log what it does. Otherwise, there are log files (/var/log/cron*). It seems to be a bit pointless of writing a program to do what cron already does. Oh, and of course, cron parses crontabs as well....
Re: Parsing and monitoring Crontabs
by jmcnamara (Monsignor) on Jul 12, 2010 at 13:21 UTC

    It isn't really clear what you want to do but Sean Burke's crontab2english might help you in some way.

    --
    John.

Re: Parsing and monitoring Crontabs
by toolic (Bishop) on Jul 12, 2010 at 13:46 UTC
    Config::Crontab has a read method for parsing a crontab file. This may be a piece of your puzzle.
Re: Parsing and monitoring Crontabs
by JSchmitz (Canon) on Jul 12, 2010 at 15:03 UTC
    First of all welcome to Perl!

    I have to say that I agree with JavaFan seems like you are re-inventing the wheel here not only can you look at the logs you can have status emailed to you
    cmd | mail -s "Subject of mail" user

    If you wish to mail the output to someone not located on the machine, in the above example, substitute user for the email address of the person who wishes to receive the output.

    If you have a command that is run often, and you don't want to be emailed the output every time, you can redirect the output to a log file (or /dev/null, if you really don't want the output). e,g

    cmd >> log.file
    Notice we're using two > signs so that the output appends the log file and doesn't clobber previous output. The above example only redirects the standard output, not the standard error, if you want all output stored in the log file, this should do the trick:
    cmd >> logfile 2>&1

    You can then set up a cron job that mails you the contents of the file at specified time intervals, using the cmd:
    mail -s "logfile for cmd" <log.file

    Have fun!! oh and s/PERL/Perl

    Cheers!!

    Jeffery
      Thank you to everyone that replied I will try your suggestions and let you know.