brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: disown lets a running job survive closing the terminal

Terminal
~/tips/tip-shell-disown-detach-job

disown -h %1

Marks a running background job so it keeps going after you close the shell.

It always happens after you start something long: the command is already running in the foreground and you realise you did not use nohup. Bash has a rescue for that.

Suspend the job with Ctrl+Z, resume it in the background and then tell the shell to leave it alone:

^Z
bg
disown -h %1

bg resumes the stopped job in the background. The -h option keeps job 1 in the job table but marks it so that it does not get a SIGHUP when the shell receives one. Without -h, a plain disown %1 removes the job from the table altogether. Use jobs first to see the job numbers:

jobs -l
disown -h %2

With -a and no job spec it applies to all jobs, and with -r only to the running ones.

The process still writes to the terminal it was started in. Once that terminal is gone, those writes can fail, and a program that gets an error on its output may stop. For something chatty, redirect the output when you start it, or run it in tmux from the beginning. So disown suits the quiet job you forgot to protect.

It also cannot rescue a program that waits for keyboard input, since nothing will ever type again. And a job that was started from a script rather than from your shell is not in your job table, so disown will not find it.

Further reading: bash(1) manual page

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link