1

I am trying to write a single line command to run a shell script which is inside the pod

getting a shell for a running container:

kubectl exec -it test-pod -c test-container -- /bin/bash

directory in the container:

cd test/bin

script inside the bin:

./backup.sh

how do I write all this in a single command?

Devaddy
  • 45
  • 6
  • 4
    When you execute `kubectl exec -it test-pod -c test-container -- /bin/bash` you are passing in the command `/bin/bash` which returns the shell. Swap that with `/bin/bash test/bin/backup.sh` – JNevill Sep 29 '22 at 18:59
  • This didn't work as it throws the error /bin/bash: test/bin/backup.sh: No such file or directory – Devaddy Sep 29 '22 at 20:29
  • Likely you'll need to supply the full path to your shell script. `/bin/bash /full/path/to/test/bin/backup.sh` – JNevill Sep 30 '22 at 14:21

2 Answers2

0

Try:

kubectl exec -it test-pod -c test-container -- sh /full/path/to/the/backup.sh

gohm'c
  • 13,492
  • 1
  • 9
  • 16
  • While correct, OP's question indicates they are using `bash` and not `sh`, not to mention there are some differences with how `sh` and `bash` function and a script that works fine under `bash` may not work as expected under `sh` – Blender Fox Sep 30 '22 at 06:51
0

Try:

kubectl exec -it test-pod -c test-container -- /bin/bash -c "/path/to/backup-script.sh"

Blender Fox
  • 4,442
  • 2
  • 17
  • 30