Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a tool to graph, analyze and help optimize crontab execution?

To clarify, I'm thinking of a tool which would generate a graph of when cron jobs are executed, and help the sysadmin reorganize them smartly.

share|improve this question

closed as not constructive by mgorven, Scott Pack, Bryan, mdpc, kce Feb 5 '13 at 3:38

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

I can't think of anything off the top of my head, but it's Bank Holiday monday tomorrow, so I could have a go at writing one. – Tom O'Connor Apr 8 '12 at 9:54

The only thing I've done is move cron jobs into a structured job scheduler in order to graph dependencies and gain visibility into the effects downtime windows.

share|improve this answer
That looks interesting but quite heavy to implement. – ℝaphink Nov 28 '11 at 13:36
It was for a 1200-line crontab I was using... but yes, very heavy. I'd like to see something lighter-weight as well. – ewwhite Nov 28 '11 at 13:38

Some sort of framework to assign a unique ID to each cron job and correlate that in the log file(s), and/or logging to a specific location to record runtime information (as opposed to the normal output log). It's not going to be trivial however you design it, but for small systems it is easy to eyeball this from looking at your crontabs and log files.

I assume you're not talking about small systems, though.

share|improve this answer
I like this idea and it could be relatively easily implemented in cron itself rather than a wrapper/framework. Just md5sum the crontab line for the unique identifier and track SIGCHILD to log stop times. – Mark Wagner Apr 3 '12 at 7:47

Sounds like this is the functionality you'd like

I can't vouch for the above, it is just the results of some web searching.

share|improve this answer

A script for print all system tasks sorted by time



tab=$(echo -en "\t")

function clean_cron_lines() {
    while read line ; do
        echo "${line}" |
            egrep --invert-match '^($|\s*#|\s*[[:alnum:]_]+=)' |
            sed --regexp-extended "s/\s+/ /g" |
            sed --regexp-extended "s/^ //"

function lookup_run_parts() {
    while read line ; do
        match=$(echo "${line}" | egrep -o 'run-parts (-{1,2}\S+ )*\S+')

        if [[ -z "${match}" ]] ; then
            echo "${line}"
            cron_fields=$(echo "${line}" | cut -f1-6 -d' ')
            cron_job_dir=$(echo  "${match}" | awk '{print $NF}')

            if [[ -d "${cron_job_dir}" ]] ; then
                for cron_job_file in "${cron_job_dir}"/* ; do  # */ <not a comment>
                    [[ -f "${cron_job_file}" ]] && echo "${cron_fields} ${cron_job_file}"

temp=$(mktemp) || exit 1

cat "${CRONTAB}" | clean_cron_lines | lookup_run_parts >"${temp}" 

cat "${CRONDIR}"/* | clean_cron_lines >>"${temp}"  # */ <not a comment>

while read user ; do
    crontab -l -u "${user}" 2>/dev/null |
        clean_cron_lines |
        sed --regexp-extended "s/^((\S+ +){5})(.+)$/\1${user} \3/" >>"${temp}"
done < <(cut --fields=1 --delimiter=: /etc/passwd)

cat "${temp}" |
    sed --regexp-extended "s/^(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(\S+) +(.*)$/\1\t\2\t\3\t\4\t\5\t\6\t\7/" |
    sort --numeric-sort --field-separator="${tab}" --key=2 --key=1 |
    sed "1i\mi\th\td\tm\tw\tuser\tcommand" |
    column -s"${tab}" -t

rm --force "${temp}"
share|improve this answer

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