<p>I'd like to get information about k8s cronjob time.
There are so many jobs in my k8s program.
So It's hard to count what time there are focused on.
I want to distribute my jobs evenly.
Is there are way to count cronjob time or sort by time?</p>

SungsooKim
<p>I have tried to find a suitable tool that can help with your case.
Unfortunately, I did not find anything suitable and easy to use at the same time.</p>
<p>It is possible to use <code>Prometheus + Grafana</code> to monitor <code>CronJobs</code> e.g using this <a href="https://grafana.com/grafana/dashboards/10884" rel="nofollow noreferrer">Kubernetes Cron and Batch Job monitoring</a> dashboard.<br />
However, I don't think you will find any useful information in this way, just a dashboard that displays the number of <code>CronJobs</code> in the cluster.</p>
<hr />
<p>For this reason, I decided to write a Bash script that is able to display the last few <code>CronJobs</code> run in a readable manner.</p>
<p>As described in the <a href="https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/" rel="nofollow noreferrer">Kubernetes CronJob documentation</a>:</p>
<blockquote>
<p>A CronJob creates Jobs on a repeating schedule.</p>
</blockquote>
<p>To find out how long a specific <a href="https://kubernetes.io/docs/concepts/workloads/controllers/job/" rel="nofollow noreferrer">Job</a> was running, we can check its <code>startTime</code> and <code>completionTime</code> e.g. using the commands below:</p>
<pre><code># kubectl get job &lt;JOB_NAME&gt; --template '{{.status.startTime}}'      #  &quot;startTime&quot;
# kubectl get job &lt;JOB_NAME&gt; --template '{{.status.completionTime}}' # &quot;completionTime&quot;
</code></pre>
<p>To get the duration of <code>Jobs</code> in seconds, we can convert <code>startTime</code> and <code>completionTime</code> dates to <strong>epoch</strong>:</p>
<pre><code># date -d &quot;&lt;SOME_DATE&gt; +%s
</code></pre>
<p>And this is the entire Bash script:<br />
<strong>NOTE:</strong> We need to pass the namespace name as an argument.</p>
<pre><code>#!/bin/bash
# script name: cronjobs_timetable.sh &lt;NAMESPACE&gt;
namespace=$1

for cronjob_name in $(kubectl get cronjobs -n $namespace --template '{{range .items}}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}'); do
    echo &quot;===== CRONJOB_NAME: ${cronjob_name} ===========&quot;

    printf &quot;%-15s %-15s %-15s %-15s\n&quot; &quot;START_TIME&quot; &quot;COMPLETION_TIME&quot; &quot;DURATION&quot; &quot;JOB_NAME&quot;
    for job_name in $(kubectl get jobs -n $namespace --template '{{range .items}}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}' | grep -w &quot;${cronjob_name}-[0-9]*$&quot;); do

        startTime=&quot;$(kubectl get job ${job_name} -n $namespace --template '{{.status.startTime}}')&quot;
        completionTime=&quot;$(kubectl get job ${job_name} -n $namespace --template '{{.status.completionTime}}')&quot;
        if [[ &quot;$completionTime&quot;  == &quot;&lt;no value&gt;&quot; ]]; then
            continue
        fi
        duration=$[ $(date -d &quot;$completionTime&quot; +%s) - $(date -d &quot;$startTime&quot; +%s) ]
        printf &quot;%-15s %-15s %-15s %-15s\n&quot; &quot;$(date -d ${startTime} +%X)&quot; &quot;$(date -d ${completionTime} +%X)&quot; &quot;${duration} s&quot; &quot;$job_name&quot;
    done
done
</code></pre>
<p>By default, this script only displays the last three <code>Jobs</code>, but it may by modified in the Job configuration using the <code>.spec.successfulJobsHistoryLimit</code> and <code>.spec.failedJobsHistoryLimit</code> fields (for more information see <a href="https://kubernetes.io/docs/tasks/job/automated-tasks-with-cron-jobs/#jobs-history-limits" rel="nofollow noreferrer">Kubernetes Jobs History Limits</a>)</p>
<p>We can check how it works:</p>
<pre><code>$ ./cronjobs_timetable.sh default
===== CRONJOB_NAME: hello ===========
START_TIME      COMPLETION_TIME DURATION        JOB_NAME
02:23:00 PM     02:23:12 PM     12 s            hello-1616077380
02:24:02 PM     02:24:13 PM     11 s            hello-1616077440
02:25:03 PM     02:25:15 PM     12 s            hello-1616077500
===== CRONJOB_NAME: hello-2 ===========
START_TIME      COMPLETION_TIME DURATION        JOB_NAME
02:23:01 PM     02:23:23 PM     22 s            hello-2-1616077380
02:24:02 PM     02:24:24 PM     22 s            hello-2-1616077440
02:25:03 PM     02:25:25 PM     22 s            hello-2-1616077500
===== CRONJOB_NAME: hello-3 ===========
START_TIME      COMPLETION_TIME DURATION        JOB_NAME
02:23:01 PM     02:23:32 PM     31 s            hello-3-1616077380
02:24:02 PM     02:24:34 PM     32 s            hello-3-1616077440
02:25:03 PM     02:25:35 PM     32 s            hello-3-1616077500
===== CRONJOB_NAME: hello-4 ===========
START_TIME      COMPLETION_TIME DURATION        JOB_NAME
02:23:01 PM     02:23:44 PM     43 s            hello-4-1616077380
02:24:02 PM     02:24:44 PM     42 s            hello-4-1616077440
02:25:03 PM     02:25:45 PM     42 s            hello-4-1616077500
</code></pre>
<p>Additionally, you'll likely want to create exceptions and error handling to make this script work as expected in all cases.</p>
