5

I'm doing a research project that requires I monitor cron jobs on a Ubuntu Linux system. I have collected data about the jobs' tasks and when they are started, I just don't know of a way to monitor how long they take to finish running.

I could calculate the time of finishing the task minus starting it with something like this but that would require doing that on the Shell scripts of each cron job. That's not necessarily difficult by any means but it seems a little silly that cron wouldn't in some way log this, so I'm trying to find an easier way :P

tl;dr Figure out time cron jobs take from start to finish

1
  • A lot of the service stuff is version-specific, especially cron. 14.04 does it differently than 16.04, and a 14.04 system that has been upgraded to 16.04 is more like a 14.04 system. It is difficult to answer this unless it is specified what exact kind of system is being discussed.
    – SDsolar
    Oct 21, 2017 at 6:30

4 Answers 4

12

You could just put time in front of your crontabs, and if you're getting notifications about cron script outputs, it'll get sent to you.

For example, if you had:

0 1,13 * * * /maint/run_webalizer.sh

add time in front

0 1,13 * * * time /maint/run_webalizer.sh

and you'll get some output that looks like (the "real" is the time you want):

real    3m1.255s
user    0m37.890s
sys     0m3.492s

If you don't get cron notifications, you can just pipe the output to a file.

5
  • This looks like it will work! My question is probably out of stupid inexperience with Linux/Shell/etc., but I need to know one more thing... since these tasks are running without me creating them (they're the default tasks on my machine that were added in /etc/crontab), where would this get printed out for me to see? Would the job open a Terminal automatically? Thank you :) Aug 15, 2012 at 17:55
  • 1
    @connergdavis I like said, usually cron implementations will email what gets printed out (stdout) from a cron job to root or some administrator email address. If you don't have something like that set up, you can try to pipe > the output to a file: time /maint/run_webalizer.sh > /maint/webalizer_out
    – Jon Lin
    Aug 15, 2012 at 17:59
  • 3
    The timing information gets printed to standard error so you want to make that ... 2>/maint/webalizer_out.
    – tripleee
    Aug 16, 2012 at 3:36
  • If you want both the program output plus the timing output, just as it would show on a terminal, then you would want to use &>maint/webalizer_out
    – SDsolar
    Oct 21, 2017 at 6:49
  • That's a Bashism, and not properly portable to standard sh, which is what crontab needs to use. The standard, portable way to redirect both standard output and standard error to a file is command >file 2>&1
    – tripleee
    Oct 21, 2017 at 9:31
1

man time. Maybe you can create a wrapper and tell Cron to use it as your "shell" or something like that.

1
  • Note the comments after the accepted answer. &> is the way to capture it all just as if it were run in a terminal window.
    – SDsolar
    Oct 21, 2017 at 6:55
1

Cronitor (https://cronitor.io) is a tool I built exactly for this purpose. It uses http requests to record the start and end of your jobs.

You'll be notified if your job doesn't run on schedule, or if it runs for too long/too short. You can also configure it to send alerts to you via email, sms, but also Slack, Hipchat, Pagerduty and others.

0

I use the Jenkins CI to do this via its external-monitor-job plugin. Jenkins can track start and end times, track overall execution time over time, save the output of all jobs it tracks, and present success/failure conditions graphically.

https://wiki.jenkins-ci.org/display/JENKINS/Monitoring+external+jobs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.