> For the complete documentation index, see [llms.txt](https://docs.mithril.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mithril.ai/mithril-cli/task-yaml/detect-idle-instances.md).

# Detect idle instances

Automatically pause your bid when your instances become idle to prevent unnecessary spend.

[Pausing your spot bid](https://docs.mithril.ai/compute-and-storage/spot-bids#pausing-and-resuming-spot-bids) releases the instances and stops billing but keeps the cluster configuration and boot disk so it can resume later.

#### For a new cluster

Add `autostop` to your task YAML:

```yaml
resources:
  # Pause bid after 10 minutes of inactivity.
  autostop: 10m
```

#### For an existing cluster

Enable autostop:

```bash
ml sky autostop my-cluster -i 10
```

Disable autostop:

```bash
ml sky autostop my-cluster --cancel
```

#### What counts as idle?

A cluster is considered **idle** when:

* No jobs are **pending or running** (check `ml queue`)
* No **active SSH sessions** exist

Once the cluster remains idle for the configured duration, a daemon inside the VM automatically pauses the bid.

### Advanced configuration

#### **Customize idleness criteria**

You can change what counts as "activity" using `wait_for`.

```yaml
resources:
  autostop:
    idle_minutes: 10
    wait_for: jobs_and_ssh
```

| Option                   | Behavior                                                                       |
| ------------------------ | ------------------------------------------------------------------------------ |
| `jobs_and_ssh` (default) | Wait for jobs in pending/running state and SSH sessions to finish              |
| `jobs`                   | Ignore SSH sessions (useful if you keep long-running IDE/SSH connections open) |
| `none`                   | The bid will pause after the configured time regardless of cluster activity.   |

The `none` option is useful for enforcing a **hard time limit**, even if workloads like Jupyter notebooks are still running

#### Run commands before autostop

You can specify commands to run before your bid is paused.

```
resources:
  autostop:
    idle_minutes: 10
    hook: |
      cd /path/to/project
      git add .
      git commit -m "Commit my code"
      git push
    hook_timeout: 300
```

The hook runs **on the cluster** and has access to its filesystem and environment variables.

If the hook exits with a non-zero status, autostop will still proceed, but a warning will be logged.

#### **Hook timeout**

By default, autostop hooks have a 1 hour timeout.

If the hook exceeds this limit, it will be terminated and autostop will continue.

Note that **your bid will not be paused until the hook finishes or times out**, so choose an appropriate `hook_timeout`.

<sub>The autostop functionality is inherited from the SkyPilot</sub>\ <sub>project. This page is largely adapted from the</sub> [<sub>SkyPilot autostop documentation</sub>](https://docs.skypilot.co/en/latest/reference/auto-stop.html)<sub>.</sub>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.mithril.ai/mithril-cli/task-yaml/detect-idle-instances.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
