20 years of Linux and I finally started using job control
And it’s amazing
I have been using Linux since before I owned my own computer, and for much of that time, have been comfortable with the command line (I'm a computer programmer, after all; though not a particularly fancy one).
I have always been annoyed that Ctrl+Z doesn’t work in terminal (not to mention, Ctrl+S, and all other table-stakes keybindings. Keybindings should be universal, man!). I knew, vaguely, what it did. It didn't sound interesting to me.
I slowly warmed up to GNU readline keybindings. They’re not as good as universal keybindings, but I sure miss them when they're gone, and now I've started using them when I write TUI dashboards. And I spend my time in a console text editor, in a non-standard shell, in tmux. Which is just to say, I'm not a complete rube.
But, job control? “Wuts job control?” said the self-styled CLI enthusiast. I knew, vaguely, that jobs could be backgrounded (whatever that meant), but I frankly didn't see the point. If you need to run something in the background, just use command & like a normal person (and then watch it rudely spit characters into your terminal when it feels like it anyhow).
So.
Then I was working on a project in which I spent large swaths of my day SSH'ed into an underpowered box running way to many heavy processes, via a bastion server. The nature of the work required binocular vision, so — prepare for the yuck — I would split my home Tmux into two panes, open two SSH sessions, knowing vaguely how gross that was, and double-hop to the server, using one pane for my main work, and the other for poking around the file system as needed.
Probably a month into the project, which involved running interminable Python analysis scripts against a 20gb SQLite DB, I (re?) read the wonderful post Shell Tricks That Actually Make Life Easier (And Save Your Sanity). There was that little fg thing again...
Suddenly, I was able to run one SSH session. Mind blown!
It works like this:
- Start long running process:
python3 my_epic_analysis_script.py --do-all-the-extra-stuff-too - Background it with Ctrl+z (and
bgif you want to to keep going rather than go on ice) - Do other stuff, like poking around the file system, or madly trying to recover from swap thrashing
- When ready to check if it finished (we're not fancy enough to have it notify), run
fgto bring it back
This is amazing right on its own, but it gets even better. Did you know that modern CPUs with modern CLIs can run two programs at once?!
- Start long running process:
python3 oh_my_gosh_this_is_going_to_take_all_night.py --auto-resume-when-you-crash - Background it with Ctrl+z (and
bg) - Start another thing that’s super slow and resource hungry:
emacs - Background that too with Ctrl+z (and
bg) - Where did all my stuff go? Someone tidied up my room and now I can't find the clothes I left on the floor! That’s a job for
jobs - Which one do we want? The first one, or the second one? The first one:
fg %1(← unclear what the percentage sign is up to but there it is) - Still not done! Ctrl+z and
bg, thenfg(we can skip the%2because there is no third job at present), then Ctrl+z andbg, then, in the morning,fg %1.
I sometimes wonder if someone who really knew Linux would ever need to install applications. The problem, often, isn’t that the feature isn’t there: the problem is discoverability.
It was the same thing when I was learning Vim. I would install all kinds of glorious plugins, and then, months or years later, learn that the base install provided 98% of the functionality I needed, it just was a bit weird, and hard to find.
Now you can press ctrl+z on this blog, and fg on whatever you were trying to do when you found it.