# Run commands in the background
<subtitle>Run a long-running command in the background and terminate it when needed. </subtitle>

To run a command in the background, pass the `background` option to the `commands.run()` method. The method returns immediately and the command continues to run in the sandbox. You can then terminate the command using the `commands.kill()` method.

```python
from ucloud_sandbox import Sandbox

sandbox = Sandbox.create()

# Start the command in the background
command = sandbox.commands.run('echo hello; sleep 10; echo world', background=True)

# Get the stdout and stderr of the background running command.
# You can run this code in a separate thread or use command.wait() to wait for the command to complete.
for stdout, stderr, _ in command:
    if stdout:
        print(stdout)
    if stderr:
        print(stderr)

# Terminate command
command.kill()
```
