Seems that kubectl logs
doesn't support cronjob. It says
error: cannot get the logs from *v1beta1.CronJob: selector for *v1beta1.CronJob not implemented
Currently I check the logs of all relative jobs one by one.
Is there any simple command or tool to get these logs?
I did some research on bash script and modified edbighead's answer to better suit my needs.
# cronJobGetAllLogs.sh: Get all logs of a cronjob.
# example:
# ./cronJobGetAllLogs.sh [Insert name of cronJob]
jobs=( $(kubectl get jobs --no-headers -o custom-columns=":metadata.name" | awk "/$1-[0-9]+/{print \$1}" | sort -r ) )
for job in "${jobs[@]}"
do
echo Logs from job $job
pod=$(kubectl get pods -l job-name=$job --no-headers -o custom-columns=":metadata.name")
kubectl logs $pod
done
# cronJobGetLatestLog.sh: Get log of latest job initiated by a cronjob.
# example:
# ./cronJobGetLateestLog.sh [Insert name of cronJob]
job=$(kubectl get jobs --no-headers -o custom-columns=":metadata.name" | awk "/$1-[0-9]+/{print \$1}" | sort -r | head -1)
pod=$(kubectl get pods -l job-name=$job --no-headers -o custom-columns=":metadata.name")
kubectl logs $pod
Deciphering your cron job logsCron logs store the time the job was started, the user account used, and the command run. In this example, a data-pipeline.py cron job ran on March 29th, on the data-processing host as the dataproc user. As valuable as cron logs are they can't tell you what cron jobs didn't run.
If you have all the pods started and running fine. Then you can check the logs of your pod by using kubectl logs <pod_name> syntax.
In cPanel there is no feature to view the cron logs files using a built-in feature. However, by using the terminal feature, you can view the cron log files using cPanel. To use this feature, you will need WHM access and the terminal feature enabled. This kind of access is only available to server owners.
From documentation of CronJobs and Jobs
A Cron Job creates Jobs on a time-based schedule
...
A job creates one or more pods and ensures that a specified number of them successfully terminate.
All you need is to view logs for a pod that was created for the job.
Find your job with kubectl get jobs
. This will return your CronJob name with a timestamp
Find pod for executed job kubectl get pods -l job-name=your-job-@timestamp
Use kubectl logs your-job-@timestamp-id
to view logs
Here's an example of bash script that does all the above and outputs logs for every job's pod.
jobs=( $(kubectl get jobs --no-headers -o custom-columns=":metadata.name") )
for job in "${jobs[@]}"
do
pod=$(kubectl get pods -l job-name=$job --no-headers -o custom-columns=":metadata.name")
kubectl logs $pod
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With