Kubectl create job from cronjob and override args
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Kubernetes lets you manually trigger a Job from an existing CronJob using kubectl create job --from=cronjob/<name>. This is useful for testing, one-off runs, or running a CronJob's task with different arguments without waiting for the schedule. However, kubectl create job --from does not directly support overriding command args. To override arguments, you must extract the CronJob's job template, modify it, and apply it manually.
Create a Job from a CronJob
This creates a Job with the exact same spec as the CronJob's job template — same container image, command, args, volumes, and environment variables.
Override Args (Extract and Modify)
kubectl create job --from does not support -- to pass command overrides. To override args, extract the job template and modify it:
A cleaner approach using kubectl create job --from --dry-run:
Override with a One-Liner (jq/yq)
Using yq (YAML processor) to override args inline:
Using jq (JSON processor):
Override Environment Variables
Example CronJob and Manual Override
Cleaning Up Manual Jobs
Checking CronJob History
Common Pitfalls
- Job name conflicts:
kubectl create jobfails if a job with the same name already exists. Always use a unique name (e.g., include a timestamp:my-job-$(date +%s)) or delete the previous job first. - Assuming
--fromsupports arg overrides:kubectl create job --from=cronjob/x -- custom-argdoes not work. The--flag is not supported with--from. You must use the dry-run + yq/jq pipeline to modify args. - Forgetting to clean up manual jobs: Manual jobs persist after completion. Unlike CronJob-created jobs (which respect
successfulJobsHistoryLimit), manually created jobs are not automatically cleaned up. UsettlSecondsAfterFinishedor manual deletion. - CronJob's
concurrencyPolicynot applying to manual jobs: TheconcurrencyPolicy(Forbid, Replace) only applies to jobs created by the CronJob scheduler. Manually created jobs always run regardless of this policy, potentially causing conflicts. - Missing RBAC permissions: Creating jobs requires
createpermission on thejobsresource. In restricted namespaces, your service account may have permission to view CronJobs but not create Jobs.
Summary
- Use
kubectl create job name --from=cronjob/nameto manually trigger a CronJob - Override args by piping
--dry-run=client -o yamlthroughyqto modify the YAML before applying - Use
yq '.spec.template.spec.containers[0].args = [...]'to set custom arguments - Set
ttlSecondsAfterFinishedon manual jobs for automatic cleanup - Manual jobs are not subject to the CronJob's
concurrencyPolicyor history limits - Always use unique job names to avoid conflicts with existing jobs

