skip to content
Alvin Lucillo

Run kubectl port-forward in background

/ 1 min read

💻 Tech

Use nohup or ‘no hung up` command to let port-forward to run in the background even after the terminal is closed. You can use this, for example, if you have script that automates some steps, including exposing ports like the port for mongodb. You don’t always want to see its output or keep an open terminal for that.

 nohup kubectl port-forward svc/some-db 27017:27017

Since the process is running in the background, if you decide to terminate it, find the process and terminate it with the process id.

ps aux | grep 'kubectl port-forward'
kill 12345