Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to kill the port-forwarding processes running in background kubernetes

Tags:

linux

I have forward all postgres service with below command

kubectl port-forward svc/data-postgres 5432:5432 &

I now want to kill this process. I tried the below command:

ps ax | egrep port-forward | egrep 'postgres' | sed 's/^\s*//' | cut -d' ' -f1 | xargs kill
Usage:
 kill [options] <pid> [...]

Options:
 <pid> [...]            send signal to every <pid> listed
 -<signal>, -s, --signal <signal>
                        specify the <signal> to be sent
 -l, --list=[<signal>]  list all signal names, or convert one to a name
 -L, --table            list all signal names in a nice table

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see kill(1).

This is giving me error. How should I proceed?

like image 969
Priya Avatar asked Jan 25 '23 04:01

Priya


2 Answers

When you use the & operator in *nix like operating systems it usually means the command will start as a background job. The output of the command will be in the format of [<job_id>] <pid>. you can see this job and all of the other jobs running with the jobs command and kill a job using kill %<job_id>. for example:

kubectl port-forward svc/data-postgres 5432:5432 & 

will return something like: [1] 1904

at any time you can inspect the running jobs:

jobs

that will return the job_id its state and the process it runs.

you can then kill this job with the kill command as follows:

kill %1 

I am sure that with this knowledge you will be able to build a better one liner for you need.

for more information see:

  • https://unix.stackexchange.com/questions/86247/what-does-ampersand-mean-at-the-end-of-a-shell-script-line
  • https://tldp.org/LDP/abs/html/x9644.html
like image 44
master o Avatar answered Feb 05 '23 18:02

master o


That's quite a bash string you have there! Good job crafting it but there are much easier ways. Namely:

pgrep kubectl | xargs kill -9

Another solution available on some distros is pkill. Which automates this a bit:

pkill kubectl

Alternatively, you could bring the job back to the foreground with the fg command. And then use ctrl+c to kill it normally.

like image 103
TJ Zimmerman Avatar answered Feb 05 '23 18:02

TJ Zimmerman