Skip to content

Add docs for jobs#1826

Merged
132ikl merged 4 commits into
nushell:mainfrom
cosineblast:add-job-docs
Mar 30, 2025
Merged

Add docs for jobs#1826
132ikl merged 4 commits into
nushell:mainfrom
cosineblast:add-job-docs

Conversation

@cosineblast
Copy link
Copy Markdown
Contributor

This adds a book entry for background job handling in nushell, with regard to how it is currently implemented.

Quick question: How do I generate the .md files for new commands such as job spawn?

@fdncred
Copy link
Copy Markdown
Contributor

fdncred commented Mar 3, 2025

Quick question: How do I generate the .md files for new commands such as job spawn?

You don't need to do that. @hustcer generates all the commands shortly after each release.

@cosineblast cosineblast marked this pull request as ready for review March 4, 2025 02:37
@132ikl
Copy link
Copy Markdown
Member

132ikl commented Mar 4, 2025

i'll take a look when i get the chance

Copy link
Copy Markdown
Member

@132ikl 132ikl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for writing this up! sorry I just got around to this, I completely forgot about this one 😅

i have a few suggestions, let me know what you think

Comment thread book/background_task.md Outdated

1. Using a third-party task management tools, like [pueue](https://github.com/Nukesor/pueue)
2. Using a terminal multiplexer, like [tmux](https://github.com/tmux/tmux/wiki) or [zellij](https://zellij.dev/)
Nushell currently presents experimental support for thread-based background jobs.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this line to above the "Spawning Jobs" header?

also, nitpick, but not sure about "presents" here, I think just "has" works fine

Comment thread book/background_task.md Outdated
Comment on lines +26 to +29
::: tip Note
Unlike many other shells, Nushell jobs are **not** separate processes, and are instead implemented
as background threads. An important side effect of that, is that all background jobs terminate once the shell process exits.
:::
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you expand this into a proper (but brief) section? things I think we should specifically cover:

  • interactive vs. non-interactive job killing behavior on Nushell exit
  • explain that this is the rationale behind no disown / eventually job dispatch

Comment thread book/background_task.md Outdated

## Using Nu With Terminal Multiplexer
Active jobs can be queried with the [`job list`](/commands/docs/job_list.md) command, which returns a table with the information of the jobs which are currently executing.
Jobs can also be kiled/interrupted by using the [`job kill`](/commands/docs/job_kill.md) command, which interrupts the job's thread and kills all of the job's child processes:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Jobs can also be kiled/interrupted by using the [`job kill`](/commands/docs/job_kill.md) command, which interrupts the job's thread and kills all of the job's child processes:
Jobs can also be killed/interrupted by using the [`job kill`](/commands/docs/job_kill.md) command, which interrupts the job's thread and kills all of the job's child processes:

Comment thread book/background_task.md Outdated
Comment on lines +54 to +77
## Unix Ctrl-Z

In unix targets (namely Linux and MacOS), Nushell's background job support also integrates with the Ctrl-Z feature
of terminals (more specifically, the `SIGTSTP` signal), which allows users to suspend foreground processes into background jobs.
When a running process is suspended, it is turned into a background job of type `frozen`. A frozen job can be reanimated into foreground with the
[`job unfreeze`](/commands/docs/job_unfreeze.md) command.

```
long_running_process
# (Ctrl-Z is pressed)

job list
# => ┏━━━┳━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┓
# => ┃ # ┃ id ┃ type ┃ pids ┃
# => ┣━━━╋━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━┫
# => ┃ 0 ┃ 1 ┃ frozen ┃ [list 1 items] ┃
# => ┗━━━┻━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━┛

job unfreeze 1
# (process is brought back where it stopped)
```

If no job id is provided to `job unfreeze`, it will unfreeze the job id of the most recently frozen job.
Therefore, one can use `alias fg = job unfreeze` to achieve a behavior similar to the one of existing unix shells.
Copy link
Copy Markdown
Member

@132ikl 132ikl Mar 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Unix Ctrl-Z
In unix targets (namely Linux and MacOS), Nushell's background job support also integrates with the Ctrl-Z feature
of terminals (more specifically, the `SIGTSTP` signal), which allows users to suspend foreground processes into background jobs.
When a running process is suspended, it is turned into a background job of type `frozen`. A frozen job can be reanimated into foreground with the
[`job unfreeze`](/commands/docs/job_unfreeze.md) command.
```
long_running_process
# (Ctrl-Z is pressed)
job list
# => ┏━━━┳━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┓
# => ┃ # ┃ id ┃ type ┃ pids ┃
# => ┣━━━╋━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━┫
# => ┃ 0 ┃ 1 ┃ frozen ┃ [list 1 items]
# => ┗━━━┻━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━┛
job unfreeze 1
# (process is brought back where it stopped)
```
If no job id is provided to `job unfreeze`, it will unfreeze the job id of the most recently frozen job.
Therefore, one can use `alias fg = job unfreeze` to achieve a behavior similar to the one of existing unix shells.
## Job suspension
On Unix targets, such as Linux and macOS, Nushell also supports suspending external commands using <kbd>Ctrl</kbd>+<kbd>Z</kbd>. When a running process is suspended, it is turned into a "frozen" background job:
```nu
long_running_process # this starts running, then Ctrl+Z is pressed
# => Job 1 is frozen
job list
# => ┏━━━┳━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━━━┓
# => ┃ # ┃ id ┃ type ┃ pids ┃
# => ┣━━━╋━━━━╋━━━━━━━━╋━━━━━━━━━━━━━━━━┫
# => ┃ 0 ┃ 1 ┃ frozen ┃ [list 1 items]
# => ┗━━━┻━━━━┻━━━━━━━━┻━━━━━━━━━━━━━━━━┛
```
A frozen job can be brought back into foreground with the [`job unfreeze`](/commands/docs/job_unfreeze.md) command:
```nu
job unfreeze
# process is brought back where it stopped
```
::: tip Tip
For those familiar with other Unix shells, you can create an alias to emulate the behavior of the `fg` command:
```nu
alias fg = job unfreeze
```
:::
By default, `job unfreeze` will unfreeze the most recently frozen job. However, you can also specify the id of a specific job to unfreeze:
```nu
vim
# => Job 1 is frozen
long_running_process
# => Job 2 is frozen
job unfreeze 1
# we're back in vim
```

I tweaked this section to make it flow better, let me know what you think.

I'm not sure if we need to specify SIGTSTP specifically, I thought of moving it to a note/tip but I couldn't think of what that would look like.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks good to me! (and I'm not even the reviewer)

Comment thread book/background_task.md Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we rename the file to background_jobs.md also?

Comment thread book/background_task.md Outdated

Unlike terminal multiplexer, you don't need to attach to multiple tmux sessions, and get task status easily.
job spawn { sleep 10sec; ' inevitable' | save --append status.txt }
## => 1
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## => 1
# => 1

Comment thread book/background_task.md Outdated

Here we provide a [nushell module](https://github.com/nushell/nu_scripts/tree/main/modules/background_task) that makes working with pueue easier.
open status.txt
## => i am
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## => i am
# => i am

Comment thread book/background_task.md Outdated
4. Add a line to the `$nu.config-path` file: `use task.nu`
5. Restart Nushell.
open status.txt
## => i am inevitable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## => i am inevitable
# => i am inevitable

@132ikl
Copy link
Copy Markdown
Member

132ikl commented Mar 30, 2025

looks great, thanks!

@132ikl 132ikl merged commit 1c66b39 into nushell:main Mar 30, 2025
2 checks passed
@cosineblast
Copy link
Copy Markdown
Contributor Author

i think this may have broken the link to the background tasks section sidebar

image

any ideas on how to solve this?

sholderbach added a commit to sholderbach/nushell.github.io that referenced this pull request Apr 7, 2025
@sholderbach
Copy link
Copy Markdown
Member

The sidebar config is under .vuepress/configs/sidebar which is sadly hidden from ripgrep by default. There the entries are named. (Due to the renaming the existing entry failed)

sholderbach added a commit that referenced this pull request Apr 7, 2025
* Fix sidebar for background job article

#1826 (comment)

* Also fix the German sidebar

This refers to the German translation of the old version Pre-builtin
jobs
@cosineblast
Copy link
Copy Markdown
Contributor Author

which is sadly hidden from ripgrep by default
Oh, that explains a lot, thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants