disown - macOS


Overview

The disown command in macOS is used to remove jobs from the current shell’s active job list, thereby preventing the shell from sending a HUP (hangup) signal to them when the shell exits. Often used in conjunction with job control commands like bg for background processing, disown is particularly effective for ensuring long-running or background processes continue running even after the terminal is closed.

Syntax

disown [options] [job_spec ...]
  • job_spec: Specifies the job to disown. If no job spec is provided, the default is the current job.

Options/Flags

  • -h: This option marks the job so that the shell does not send a HUP signal, but does not remove the job from the job table.
  • -a: If no job specification is present, disown all jobs; otherwise, it has no effect.
  • -r: Restricts operation to running jobs only.

Examples

  1. Disowning the Current Job

    sleep 300 &
    disown
    

    This will start sleep as a background job, then disown will remove it from the job table, allowing it to continue running after the terminal is closed.

  2. Disowning a Specific Job

    sleep 300 &
    jobs
    disown %1
    

    Here, %1 is the job number as shown in the output of jobs. This specific job will be disowned.

  3. Using the -h Option

    sleep 300 &
    disown -h %1
    

    The job will continue to appear in the jobs list and will not receive a HUP signal when the shell exits.

Common Issues

  • Error: no such job: This error occurs if the job_spec does not match any known job. Ensure that the job number corresponds to a currently running job, as seen in the jobs command output.

  • Problematic behavior when disowning suspended jobs: If a job is suspended and then disowned, it might not continue running as expected unless resumed beforehand with bg or fg.

Integration

disown is often used in scripts or terminal sessions to ensure critical processes are not terminated with the session end. It can be paired with nohup for further assurance against hangups:

nohup ./long_running_script & disown

Here, nohup starts the script by ignoring the HUP signal, and disown removes the job from the job list, detaching it entirely from the terminal.

  • jobs – Lists the jobs running in the current session along with their statuses.
  • bg – Resumes a suspended job by running it in the background.
  • fg – Brings a background job to the foreground.
  • nohup – Runs a command immune to hangups, with output to a non-tty.

For further reading and more detailed information, visit the Bash Reference Manual.