0

I'm trying to automate these steps using a bash script

  1. Run a command to access the shell of a program (bash shell of a Kubernetes pod)

    kubectl exec --stdin --tty hello-node-7567d9fdc9-zvz55 -- bash
    
  2. In this shell, run a second command (eg. python)

    root@hello-node-7567d9fdc9-zvz55:/# python
    
    Python 3.7.4 (default, Aug 13 2019, 15:17:50) 
    [Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    
  3. Keep the shell open/running in the foreground (so more commands can immediately be entered by a user into the python shell started in step 2)

I'm able to do the first step described above using the script below. How do we do the second and third step?

#! /bin/bash
bash -c 'kubectl exec --stdin --tty hello-node-7567d9fdc9-zvz55 -- bash'
4
  • you need to write a pexpect script Commented Sep 29, 2021 at 19:54
  • Or use python -c 'print("hello from python"); print ("hi")' for supplying multiple python commands Commented Sep 29, 2021 at 20:34
  • You can try to run individual commands in a pod, also can be useful to check this answer. Commented Sep 29, 2021 at 21:51
  • You can replace bash -c 'kubectl …' with just kubectl …: there’s absolutely no reason to spawn yet another shell process inside your shell script. Commented Sep 30, 2021 at 15:17

1 Answer 1

0

For step 2 you can use the nohup command to run a process and even if the user logs off the process does not get killed. Afterwards the shell is kept running for you to perform step 3

Sign up to request clarification or add additional context in comments.

2 Comments

I'll like the bash script to run steps 1-3 and end up with the python shell open in the foreground. I think nohup keeps the process running but keeps it in the background
Ok so once you have the interactive Bash session which is step 1 you can start Python. If Python is not installed, you can install Python as normal, as if you had a normal Linux server and you have logged in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.