For when I forget to start tmux and realize it 6 hours into my 12 hour dd command…

References

Keyboard

  • <C-c> kill running job (stop it completely)
  • <C-z> suspend running job (pause it, can resume later)
  • <C-s> pause output from current job (job continues)
  • <C-q> resume output from previously paused job output

Commands

Suffix any command with ampersand (&) to send it to the background when you run it. Stdout still displays on the screen (messy) but you can type commands.

Can omit %n, and it will simply operate on the most recent job instead of the specified job.

  • jobs: list the #, status, and name of jobs associated to the current tty (pass -l or -p flag to get PID)
  • bg %n: send job n to the background
  • fg %n: send job n to the foreground
  • disown %n: disassociate job n from the tty (pass -h flag to leave it in the list of jobs, but still suppress SIGHUP)
  • kill %n: kill job n (there’s way more to this one, with sending specific signals…not going to cover it here)
  • nohup: suppress output from a job (this one is not a shell built-in)

Examples

These are just practical examples that I use, and often forget how to use.

Pause a long or continuous command to free up the terminal, and resume it afterwards.

ping vimoire.com
# ^Z (to suspend the job)
# <some other one-off command>
fg

Start a continuous or long-running job and send it to the background (frees up the tty, but any output is still printed - ping is probably not a good example)

ping vimoire.com &

Start a continuous job, send it to the background, make sure it keeps running in the event of SIGHUP (parent terminal closure), and suppress output. (You can also append output to a log file instead of sending it to the void with /dev/null, if that makes more sense to do.)

ping vimoire.com >/dev/null 2>&1 & disown
# OR
nohup ping vimoire.com & # (then ^C, because nohup prints a line notifying the output will be appended to nohup.out)

EOF