Search code examples
kuberneteskubectl

List non-suspended cronjobs using kubectl


How to select SUSPEND=False cronjob?

--selector can't be used as suspend is not added in the labels.


Solution

  • By using a JSONPath expression and passing it via the --output=jsonpath= option to kubectl I was able to select only cronjobs which are unsuspended and print their names as follows:

    kubectl get cronjob --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'
    

    In order yo invert the selection you can simply filter on spec.suspend==true instead of false, i.e.:

    kubectl get cronjob --output=jsonpath='{range .items[?(.spec.suspend==true)]}{.metadata.name}{"\n"}{end}'
    

    For explicitness you can additionally pass the --no-headers like so, even though the former command already does not print any headers:

    kubectl get cronjob --no-headers --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'
    

    Last but not least you can combine the commands above with Kubernetes selector expressions; for example:

    kubectl get cronjob --selector=sometagkey==sometagvalue --no-headers --output=jsonpath='{range .items[?(.spec.suspend==false)]}{.metadata.name}{"\n"}{end}'