> ## Documentation Index
> Fetch the complete documentation index at: https://docs.coreweave.com/llms.txt
> Use this file to discover all available pages before exploring further.

# サンドボックスを長時間実行する

> サーバーレスサンドボックスのライフタイムを最大 30 日まで設定します。

サーバーレスのサンドボックスは、最大 30 日間 (2,592,000 秒) 実行できます。エージェント、開発環境、バッチワークロードが完了するまでの十分な時間を確保できるよう、サンドボックスの作成時に有効期間を設定してください。

このガイドでは、有効期間の延長をリクエストする方法を説明します。

## Before you begin

あらかじめ[サンドボックスのセットアップ](/ja/products/sandboxes/serverless/get-started)を完了しておいてください。Python のサンプルを実行するには、`wandb` エクストラ付きの `cwsandbox` パッケージと、環境変数 `WANDB_API_KEY` に設定した W\&B APIキーが必要です。`[WANDB-API-KEY]` をご自身の W\&B APIキーに置き換えてください：

```bash theme={"system"}
pip install --upgrade 'cwsandbox[wandb]'
export WANDB_API_KEY="[WANDB-API-KEY]"
```

## Choose a lifetime

The lifetime is an upper bound on how long the sandbox can run. The platform terminates it when the lifetime expires, even if work is still in progress. A workload that finishes or a sandbox that you stop can end sooner.

| 設定 | サーバーレス |
| - | - |
| サンドボックスごとの有効期間 | ソフトウェア開発キット (SDK) またはサンドボックス作成リクエストで設定します。 |
| ランナーポリシーのデフォルト | CoreWeave が管理します。 |
| 最大ライフタイム | 30 日。 |

An explicit lifetime takes precedence over a runner-policy default. If you omit the lifetime, the platform uses the applicable policy default. If no positive default supplies a lifetime, the platform uses 10 minutes (600 seconds). Set the lifetime explicitly for long-running work instead of relying on a default.

30 日を超えるリクエストはプラットフォームで拒否されます。値に `0` を指定しても、実行時間が無制限になるわけではありません。

You can't extend the lifetime after a sandbox starts. Choose enough time at creation, and plan to save your work before the deadline.

## Set the lifetime when creating a sandbox

Python では `max_lifetime_seconds` を使用します。次の例では、上限である 30 日間をリクエストし、後で再接続できるようにサンドボックス ID を出力します：

```python title="Create a Serverless sandbox" theme={"system"}
from cwsandbox import AuthStrategy, Sandbox

sandbox = Sandbox.run(
    "sleep", "infinity",
    auth=AuthStrategy.WANDB,
    placement_mode="serverless",
    container_image="python:3.11",
    resources={"cpu": "2", "memory": "4Gi"},
    max_lifetime_seconds=30 * 24 * 60 * 60,
)
sandbox.wait()
print(sandbox.sandbox_id)
```

The script prints the sandbox ID. These examples keep the primary process alive with `sleep infinity` and leave the sandbox running after the script exits. When you finish using the sandbox, stop it.

The same lifetime is available through the other client interfaces:

| インターフェース | 有効期間の設定 |
| - | - |
| Python SDK | `Sandbox.run(max_lifetime_seconds=2592000, ...)`。クライアント全体で共有するデフォルトを設定するには、`SandboxDefaults(max_lifetime_seconds=2592000)` を使用します。引数を明示的に指定すると、クライアントのデフォルトが上書きされます。 |
| TypeScript SDK | `client.run()` に渡すオプションで `maxLifetimeSeconds: 2592000` を指定します。 |
| Compose ファイルのインポート | `POST /v1/sandboxes:createFromFile` のトップレベルのリクエストフィールド `maxLifetimeSeconds` を `2592000` に設定します。有効期間はリクエスト側の設定であり、Compose のサービスのプロパティではありません。 |

リクエストの形式については、[Compose ファイルからサンドボックスを作成する](/products/sandboxes/serverless/create-from-file)を参照してください。Python クライアントのデフォルト値については、[サンドボックスの設定](/ja/products/sandboxes/serverless/client/guides/sandbox-configuration#max_lifetime_seconds)を参照してください。

## Keep working across client sessions

When a sandbox should outlive the Python script that launches it, create a standalone sandbox, as in the preceding examples. A `with Sandbox.run(...)` context stops the sandbox when the block exits, even if its configured lifetime is 30 days. A `Session` also manages cleanup of the sandboxes it owns. See [Cleanup patterns](client/guides/cleanup-patterns).

To reconnect from another script, replace `[SANDBOX-ID]` with the ID printed at creation:

```python title="Reconnect to a running sandbox" theme={"system"}
from cwsandbox import AuthStrategy, Sandbox

sandbox = Sandbox.from_id(
    "[SANDBOX-ID]",
    auth=AuthStrategy.WANDB,
).result()
result = sandbox.exec(["echo", "Connected"]).result()
print(result.stdout)
```

The command prints `Connected`. Reconnecting doesn't reset the lifetime. When you finish, stop the sandbox explicitly:

```python title="Stop a sandbox" theme={"system"}
from cwsandbox import AuthStrategy, Sandbox

sandbox = Sandbox.from_id(
    "[SANDBOX-ID]",
    auth=AuthStrategy.WANDB,
).result()
sandbox.stop().result()
```

## Plan for expiry

サンドボックスの有効期間が切れる前に、結果やチェックポイントをサンドボックスの外部に保存してください。ワークフローに 30 日以上かかる場合は、新しいサンドボックスを作成し、保存した状態から再開してください。サポートされるストレージおよびスナップショットのオプションについては、[サンドボックスボリューム](/ja/products/sandboxes/serverless/volumes)と[ファイルシステムスナップショット](/products/sandboxes/serverless/file-system-snapshots)を参照してください。

The sandbox lifetime is separate from the timeout for a command or API request. Increasing `timeout_seconds` or `request_timeout_seconds` doesn't extend the sandbox lifetime. For the differences, see [Timeouts](client/guides/sandbox-configuration#timeouts).
