You can use the date
command to control whether or not to execute another script or command:
# Tested using GNU coreutils date 8.30/bin/sh -c "[ $(date -d +1day +%d) -eq 1 ] && echo 'It is the last day of the month!'"# It is the last day of the month!
You can use this logic and your CronJob
to run on the 28th-31st of each month, but only execute your script if tomorrow is the 1st of the month:
apiVersion: batch/v1kind: CronJobmetadata: name: hellospec: # Run at 6pm on the 28th-31st of each month schedule: "0 18 28-31 * *" jobTemplate: spec: template: spec: containers: - name: cronjob image: busybox:1.35.0 imagePullPolicy: IfNotPresent command: - /bin/sh - -c - "[ $(date -d +1day +%d) -eq 1 ] && job.sh" restartPolicy: OnFailure
Additionally, if there's a discrepancy on your version of date
inside your base image and it doesn't support -d +1day
, just offload the date check into your actual container job and have your job check to see if "tomorrow" is the 1st.