# 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>
