The command line

This page contains useful snippets of commands and reference information that we have collected. These commands have been developed and tested on a Linux / Mac command line interface.

Splitting ‘oc’ output for readability

The ‘oc’ get command can be used to view lots of information about deployments, pods, services etc. Some of the outputs are a little compressed, for example oc get endpoints displays the result as shown below.

oc get endpoints chaindemo
NAME        ENDPOINTS                                                           AGE
chaindemo   10.129.0.244:8080,10.129.0.245:8080,10.129.0.246:8080 + 2 more...   30d

A useful addition to the ‘oc get endpoints’ command is to use a filtered jsonpath output :

oc get endpoints chaindemo -o  jsonpath='{.subsets[*].addresses[*].ip}'                              
10.129.0.244 10.129.0.245 10.129.0.246 10.129.0.247 10.129.1.209      

But as you can see the output is a space separated list on the same line which may be not be very useful. To split the content such that you get a simple line per endpoint use :

oc get endpoints chaindemo -o  jsonpath='{.subsets[*].addresses[*].ip}' | tr -s " " "\012" && echo ''
10.129.0.244
10.129.0.245
10.129.0.246
10.129.0.247
10.129.1.209

Getting OpenShift events (in order)

The ‘oc get events’ command displays useful information about the creation / deletion / modification of objects within a project together with useful error messages. However, as shown below the results are not always in a particularly helpful order.

Note that some of the output has been edited slightly for readability.

oc get events
LAST SEEN   TYPE     REASON              OBJECT                           MESSAGE
8m15s       Normal   Scheduled           pod/chaindemo-1    Successfully assigned demotest/chaindemo-1 to node-1
8m8s        Normal   AddedInterface      pod/chaindemo-1    Add eth0 [10.129.0.244/23] from openshift-sdn
8m8s        Normal   Pulled              pod/chaindemo-1    Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
8m8s        Normal   Created             pod/chaindemo-1    Created container chaindemo
8m8s        Normal   Started             pod/chaindemo-1    Started container chaindemo
8m14s       Normal   Scheduled           pod/chaindemo-2    Successfully assigned demotest/chaindemo-2 to node-1
8m7s        Normal   AddedInterface      pod/chaindemo-2    Add eth0 [10.129.0.245/23] from openshift-sdn
8m7s        Normal   Pulled              pod/chaindemo-2    Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
8m6s        Normal   Created             pod/chaindemo-2    Created container chaindemo
8m6s        Normal   Started             pod/chaindemo-2    Started container chaindemo
8m14s       Normal   Scheduled           pod/chaindemo-3    Successfully assigned demotest/chaindemo-3 to node-1
8m3s        Normal   AddedInterface      pod/chaindemo-3    Add eth0 [10.129.0.247/23] from openshift-sdn
8m3s        Normal   Pulled              pod/chaindemo-3    Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
8m2s        Normal   Created             pod/chaindemo-3    Created container chaindemo
8m2s        Normal   Started             pod/chaindemo-3    Started container chaindemo
8m14s       Normal   Scheduled           pod/chaindemo-4    Successfully assigned demotest/chaindemo-4 to node-1
8m5s        Normal   AddedInterface      pod/chaindemo-4    Add eth0 [10.129.0.246/23] from openshift-sdn
8m5s        Normal   Pulled              pod/chaindemo-4    Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
8m4s        Normal   Created             pod/chaindemo-4    Created container chaindemo
8m4s        Normal   Started             pod/chaindemo-4    Started container chaindemo
8m15s       Normal   SuccessfulCreate    replicaset/chaindemo-bcbcfdc56   Created pod: chaindemo-1
8m14s       Normal   SuccessfulCreate    replicaset/chaindemo-bcbcfdc56   Created pod: chaindemo-2
8m14s       Normal   SuccessfulCreate    replicaset/chaindemo-bcbcfdc56   Created pod: chaindemo-3
8m14s       Normal   SuccessfulCreate    replicaset/chaindemo-bcbcfdc56   Created pod: chaindemo-4        

Setting the sort order and selecting specific fields of data makes the output a little more readable and useful. Newest events are at the top and older events are at the bottom. Note that the output clould be piped to the tail command to select a subset of the most recent lines.

oc get events --sort-by={.firstTimestamp} -o jsonpath='{range .items[*]}{.firstTimestamp} {.message}{"\n"}'       
2022-02-14T10:32:12Z Scaled up replica set chaindemo-bcbcfdc56 to 3
2022-02-14T10:32:12Z Created pod: chaindemo-bcbcfdc56-5vpxp
2022-02-14T10:32:12Z Scaled up replica set chaindemo-bcbcfdc56 to 2
2022-02-14T10:32:13Z Scaled up replica set chaindemo-bcbcfdc56 to 4
2022-02-14T10:32:13Z Created pod: chaindemo-3
2022-02-14T10:32:13Z Created pod: chaindemo-4
2022-02-14T10:32:13Z Created pod: chaindemo-2
2022-02-14T10:32:13Z Scaled up replica set chaindemo-bcbcfdc56 to 5
2022-02-14T10:32:19Z Created container chaindemo
2022-02-14T10:32:19Z Add eth0 [10.129.0.244/23] from openshift-sdn
2022-02-14T10:32:19Z Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
2022-02-14T10:32:19Z Started container chaindemo
2022-02-14T10:32:20Z Container image "image-registry.openshift-image-registry.svc:5000/demotest/chaindemo@sha256:ea...90" already present on machine
2022-02-14T10:32:20Z Add eth0 [10.129.0.245/23] from openshift-sdn
2022-02-14T10:32:21Z Created container chaindemo
2022-02-14T10:32:21Z Started container chaindemo
2022-02-14T10:32:22Z Add eth0 [10.129.0.246/23] from openshift-sdn

Note that the output can be piped to the tail command to reduce the result to a manageable set of most recent events if your environment has a lot of data to show.