July 3rd, 2024

Reasons to use your shell's job control

Job control in a shell environment offers benefits like managing processes in foreground, background, or stopped states. Commands like fg, bg, Ctrl+z, and kill are explained for process manipulation. Users can kill unresponsive processes, background GUI apps, manage long-running jobs, test commands, and organize output efficiently.

Read original articleLink Icon
Reasons to use your shell's job control

The article discusses the benefits of using job control in a shell environment interactively. Job control allows users to manage processes in the foreground, background, or stopped states. Commands like fg, bg, Ctrl+z, jobs, kill, disown, and wait are explained for manipulating processes. Reasons for using job control include killing unresponsive processes, backgrounding GUI apps, managing long-running jobs, testing commands while using editors like vim, and organizing interleaved output. Job control can also be useful for suspending CPU-intensive tasks, recovering accidentally stopped programs, maintaining environment variables, and when it's the only available option. Different perspectives on job control usage are shared, highlighting preferences for terminal visibility and managing multiple processes efficiently. The article concludes with the author's intention to explore job control further for specific tasks like killing unresponsive processes and running network commands alongside tools like tcpdump.

Link Icon 7 comments
By @chasil - 4 months
Historically, job control was written by Bill Joy for the csh, while he was working on a new, cheap serial terminal - the Lear-Siegler ADM-3A. This terminal is famous for defining the arrows for Joy's vi editor, and assigning the ~ tilde character as the home directory in all POSIX shells.

https://en.wikipedia.org/wiki/ADM-3A

It is unfortunate that Bill Joy was both the first and last word in job control (even as his csh has been largely abandoned), as this functionality has been copied first into the Korn shell, then into the POSIX shell, and hasn't seen any substantial improvement since the 1970s.

It would be very helpful if shell job control could address available processors (and become aware of asymmetric big/LITTLE configurations), hold jobs until CPUs become available, and keep a list of failed jobs for retry.

The standards will likely never implement this functionality. This is the fault of the Austin Group.

By @simmons - 4 months
I love job control! And as usual, Julia Evans explains it in a clear and straightforward fashion.

I would add that as a shortcut, you can just type % (without fg) to foreground the current job, or % and a number to foreground a particular job. (In Bash, anyway.)

I use tmux, but I often use job control as an extra dimension, having several jobs in any given tmux window. (Yes, this leads to the inevitable "There are stopped jobs." warning when I attempt to exit a shell/window, which makes cleanup slightly more tedious.)

By @aidenn0 - 3 months
One nitpick on disown:

It will prevent the process from getting SIGHUP when the terminal is closed, but it won't change the pty or stdin/stdout/stderr of the process, so it may still abort if/when I/O is performed. Using e.g. nohup solves this problem more completely. If you see programs abort even with disown, they are probably aborting on I/O errors to the terminal.

Also, some terminals (e.g. kitty) will not auto-close when bash exits if there are still processes having the PTY open. Here's a simple example to play around with to see some different possibilities (and check how your shell handles things). NB: It runs "exit" at the end, so run in a fresh terminal window/tab

  { set -e; rm -f /tmp/got-here; sleep 5; echo hi; touch /tmp/got-here; } 2>/tmp/test-stderr & disown; exit

If you run this in a terminal that doesn't close when your shell exits if any other process still has the pty open, then the tab/window will hang around for 5 seconds and then disappear; /tmp/got-here will exist. If you run in a terminal that does close when your shell exits (e.g. xterm), then /tmp/got-here will never be created and you'll see an IO error message in /tmp/test-stderr
By @brodouevencode - 4 months
I'm curious to see how many engineers here use shell/*nix tooling on a day to day basis. My experience has been that newer engineers have no experience nor desire to learn any of it, whereas it was basically required when I was coming up.
By @anothername12 - 4 months
The usefulness of job control to me is why I can’t stand to use PowerShell and a few other “modern” fancy shells for command line work.
By @david2ndaccount - 4 months
If a program is not responding to ctrl-c, sometimes ctrl-\ (which sends sigquit instead of sigint) can kill it.