kubectl port-forward and the lost art of bash job control

TL;DR: If you want to use a port-forward in the same shell as you created it, hit ctrl-z then run bg. You can now use the port-forward while it runs in the background. Once done, to kill the port-forward run fg to foreground it and then hit ctrl-c

A railway junction

A really common part of debugging stuff in k8s is to create a port-forward and then open a new shell and do a thing. Which is fine and easy when you’re port-forwarding to the machine you’re actually sat at, but less so when you’re not.

Fortunately, there’s a really common technique from the last time we were all working at machines we weren’t sat at; task backgrounding.

In bash, if you’re running a command in the foreground you can hit ctrl-z to pause it. This can be helpful when you’ve an intensive task running and you’d like your CPU back for a bit; once you’re done you can run fg and the task starts running again.

But you can also run bg once you’ve paused a task, and that’ll put it in the background, giving you back your shell. That backgrounded task’s output will still appear on your shell, so this used to be a common way to run tcpdump and then run the commands to cause the traffic you wanted to inspect. While the task is backgrounded you can do what you like in the shell and it’ll keep running. When you’re done, run fg to bring that backgrounded command to the foreground, and then a ctrl-c will exit it as normal.

This feature is called ‘job control’ and there’s much more to it than just backgrounding single tasks. There’s a writeup here, but there’s also a lot elsewhere: https://www.linuxjournal.com/content/job-control-bash-feature-you-only-think-you-dont-need