# Run Python Script in Background

A Python script, say a Telegram bot, for example, can be run in many different ways. While using Tmux, Screen do seem easy, what truly stand out was this command `nuhup`.

## Option 1: `nuhup`

`nuhup` can be used to run Python script in background by:

```bash
nuhup python3 {PYTHON_SCRIPT_PATH} &
```

***Note:*** `&` is important, kindly add it so that the script can run in background.

By default `nuhup` writes to `nuhup` writes to `nohup.out` which can be changed by redirecting stdout to any file, example:

```bash
nuhup ./run.py > log.txt &
```

## Option 2: `tmux`

Using `tmux` isn't that very hard, and can be done with just running `tmux` and detaching the session.

```bash
tmux
```

Then,

```bash
python3 {SCRIPT}
```

Press, `ctrl + b` and `d` after it to detach the current shell session.

## Option 3: `screen`

Using `screen` isn't that different from using `tmux`, and can be done by:

```bash
screen
```

Then,

```bash
python3 {SCRIPT}
```

Press, `ctrl + a` and `d` immediately after it to detach the current screen shell session.

***Source:*** [***Janakiev***](https://janakiev.com/blog/python-background/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://til.devjugal.com/python/run-python-script-in-background.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
