3

I am preparing for the CKAD exam and I am doing practice with the questions provide here

I have a doubt over this two different way of executing commands. Here the example provided is with a job but I think the question might be more general and extendable to all containers.

Under the job exercises there are two requests:

Create a job named pi with image perl that runs the command with arguments "perl -Mbignum=bpi -wle 'print bpi(2000)'"

kubectl create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

Create a job with the image busybox that executes the command 'echo hello;sleep 30;echo world

kubectl create job busybox --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

Why in the second command I need to provide /bin/sh -c as well?

How could I understand when to use it and when not?

Cr4zyTun4
  • 625
  • 7
  • 18
  • 3
    You are using `;` which isn't an argument to `echo` but a delimiter parsed by the shell, so you need a shell to parse and execute it. Otherwise, you'd be running `echo` with arguments `hello;sleep` and `30;echo` and `world` which is likely not what you want. – CherryDT Jul 09 '21 at 12:42
  • Thank you very much for the answer, would you like to add it as answer so that I can accept it? – Cr4zyTun4 Jul 09 '21 at 12:53

1 Answers1

2

Because in first example

kubectl create job pi  --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

you call a perl interpreter, perl -Mbignum=bpi -wle 'print bpi(2000)'

and in second example you call a bash shell command, echo hello;sleep 30;echo world therefore /bin/sh -c

kubectl create job busybox --image=busybox -- /bin/sh -c 'echo hello;sleep 30;echo world'

The -- is saying after this it is a command is executed inside the container.

bodo
  • 827
  • 6
  • 19
  • Thanks for the answer, I wanted to ask you another thing if possible. If the command is something simpler like 'sleep 3600' do I need to provide '/bin/sh -c' before or just 'sleep 3600' in the command section (of course with right syntax ["sleep","3600"] would work fine? – Cr4zyTun4 Jul 12 '21 at 15:26
  • You could look inside the container but I assume that `/bin/sh` will be just a symbolic link to `/bin/bash`. Example: `$ ls -l /bin/sh` `lrwxrwxrwx. 1 root root 4 Jun 1 2020 /bin/sh -> bash` `$ readlink -f /bin/sh` `/usr/bin/bash` So `/bin/sh` is really just an abstraction to your current shell. Beware it could also be `zsh` or `ash` etc. So if yo want that command will be executed with you currently configured shell, use `/bin/sh -c ""`. If you executed only `sleep 3600` it is possible that in some containers shell is not configured at the `PATH` so it might end with error. – bodo Jul 13 '21 at 14:28