Skip to content

Commit

Permalink
Update background_task.md (#1166)
Browse files Browse the repository at this point in the history
* Update background_task.md

* Update background_task.md
  • Loading branch information
RGBCube authored Dec 12, 2023
1 parent b513d2c commit 7b7f664
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions book/background_task.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
# Background task in Nu
# Background tasks with Nu

Currently Nushell doesn't have built-in background task management feature, but you can make it "support" background task with some tools, here are some example:
Currently, Nushell doesn't have built-in background task management feature, but you can make it "support" background task with some tools, here are some examples:

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/)
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/)

## Using nu with pueue
Borrows the power of [pueue](https://github.com/Nukesor/pueue), it is possible to schedule background tasks to pueue, and manage those tasks (such as viewing logs, killing tasks, or getting the running status of all tasks)

The module borrows the power of [pueue](https://github.com/Nukesor/pueue), it is possible to schedule background tasks to pueue, and manage those tasks (such as viewing logs, killing tasks, or getting the running status of all tasks, creating groups, pausing tasks etc etc)

Unlike terminal multiplexer, you don't need to attach to multiple tmux sessions, and get task status easily.

Here we provide a [nushell module](https://github.com/nushell/nu_scripts/tree/main/modules/background_task) to work with pueue easiler.

Here is a setup example to make nushell "support" background task:
1. install pueue
2. run `pueued` with default config, you can refer to [start-the-daemon page](https://github.com/Nukesor/pueue/wiki/Get-started#start-the-daemon) for more information.
3. put the [job.nu](https://github.com/nushell/nu_scripts/blob/main/modules/background_task/job.nu) file under `$env.NU_LIB_DIRS`.
4. add a line to the `$nu.config-path` file: `use job.nu`
5. restart nu.
Here is a setup example to make Nushell "support" background tasks:

1. Install pueue
2. run `pueued`, you can refer to [start-the-daemon page](https://github.com/Nukesor/pueue/wiki/Get-started#start-the-daemon) for more information.
3. Put the [task.nu](https://github.com/nushell/nu_scripts/blob/main/modules/background_task/task.nu) file under `$env.NU_LIB_DIRS`.
4. Add a line to the `$nu.config-path` file: `use task.nu`
5. Restart Nushell.

Then you will get some commands to schedule background tasks. (e.g: `job spawn`, `job status`, `job log`)
Then you will get some commands to schedule background tasks. (e.g: `task spawn`, `task status`, `task log`)

Cons note: It spawned a fresh nushell to execute the given command, so it doesn't inherit current scope's variables, custom commands, alias definition, except env variables which can convert value to string. Therefore, if you want to use custom commands or variables, you have to [`use`](/commands/docs/use.md) or [`def`](/commands/docs/def.md) them within the given block.
Cons: It spawns a new Nushell interpreter to execute every single task, so it doesn't inherit current scope's variables, custom commands, alias definition.
It only inherits environment variables whose value can be converted to a string.
Therefore, if you want to use custom commands or variables, you have to [`use`](/commands/docs/use.md) or [`def`](/commands/docs/def.md) them within the given block.

## Using nu with terminal multiplexer

You can choose and install a terminal multiplexer and use it.

It allows you to easily switch between multiple programs in one terminal, detach them (they continue to run in the background) and reconnect them to a different terminal. As a result, it is very flexible and usable.

0 comments on commit 7b7f664

Please sign in to comment.