3

I am trying to run multiple commands in a K8s cron job. However, only the first jobs output is showing in the logs. I have this:

          containers:
          - name: myjob
            image: postgres 
            args:
            - /bin/sh
            - -c
            - PGPASSWORD=$(echo $password) psql -h $host -p 5432 -U $username -d $db -c "\copy (select * from blah limit 1) to blah.txt" ; echo bar.txt ; cat bar.txt;
            - PGPASSWORD=$(echo $password) psql -h $host -p 5432 -U $username -d $db -c "\copy (select * from foo limit 1) to foo.txt" ; date ; echo foo.txt ; cat foo.txt;
          restartPolicy: OnFailure

I am only seeing the output from the first command. I would like to be able to specify multiple commands and run them one by one.

Anthony
  • 175
  • 1
  • 2
  • 7

2 Answers2

0

just join them in a single line after -c with && or ; operator

See the complete answer here

0

Just can run two commands by a standard procedure in Pod.

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

However, I doubt it is a good idea and it should be used as last hope. Containers are designed to run only one process and CronJobs use Pod specification.

Hence, I can recommend the following things. Order matters.

  • Run two separate CronJobs if your tasks are completely independent.
  • Run two separate containers in CronJob. The CronJob will fail, if one of your containers fail.
  • Use initContainers to achieve some order or preparation before.
limakzi
  • 31
  • 2