Priya
Priya

Reputation: 303

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

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?

Upvotes: 8

Views: 7281

Answers (2)

master o
master o

Reputation: 67

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:

Upvotes: 4

TJ Zimmerman
TJ Zimmerman

Reputation: 3484

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.

Upvotes: 14

Related Questions