# ジョブ: スケジュールと起動時

**想定読者:** エージェント起動直後にジョブを実行し、その後も固定間隔で実行し続けたい統合担当者。

スキャナーやレポーターでよくあるパターンです。起動時に一度すぐ実行してベースラインを得てから、スケジュールどおり継続します。[ジョブ: スケジュールのみ](/keeperpam/jp/endpoint-privilege-manager/integrations/examples/job-schedule-only.md)との違いは、`eventType: Startup` の `events` エントリを追加する点です。両方のトリガーは独立しています。エージェントがジョブを読み込んだタイミングでStartupイベントが1回実行され、インターバルタイマーはStartup実行の完了を待たずにその時点から進みます。

本例はWindowsのパスを使います。LinuxおよびmacOSでは、[ジョブ: 最小構成 (Linux)](/keeperpam/jp/endpoint-privilege-manager/integrations/examples/job-minimal-linux.md) および [ジョブ: 最小構成 (macOS)](/keeperpam/jp/endpoint-privilege-manager/integrations/examples/job-minimal-macos.md) の例に示すとおり、パスと `osFilter` を置き換えてください。

## ジョブのJSON <a href="#the-job-json" id="the-job-json"></a>

```json
{
  "id": "my-tool",
  "name": "My Tool",
  "description": "Runs MyTool at agent startup and every 120 minutes thereafter.",
  "enabled": true,

  "schedule": {
    "intervalMinutes": 120
  },

  "events": [
    { "eventType": "Startup" }
  ],

  "osFilter": {
    "windows": true,
    "linux": false,
    "macOS": false
  },

  "mqttTopics": {
    "allowedPublications": ["KeeperLogger"],
    "allowedSubscriptions": []
  },

  "parameters": [],

  "tasks": [
    {
      "id": "run-tool",
      "name": "Run tool",
      "ExecutionType": "Service",
      "command": "MyTool",
      "executablePath": "C:\\Program Files\\KeeperPrivilegeManager\\Jobs\\bin\\MyTool\\MyTool.exe",
      "arguments": "--keeper-api-base={KeeperApiBaseUrl}",
      "timeoutSeconds": 7200,
      "continueOnFailure": false,
      "scriptType": "Auto"
    }
  ]
}
```

## 変更する箇所 <a href="#what-to-change" id="what-to-change"></a>

| フィールド                      | 内容                                                                                                    |
| -------------------------- | ----------------------------------------------------------------------------------------------------- |
| `id`                       | 本ジョブを一意に識別する文字列。ハイフンを使い、アンダースコアは使いません。ファイル名は `id` と一致させます (例: `"id": "my-tool"` のときは `my-tool.json`)。 |
| `name`                     | ログや管理画面に表示する名称                                                                                        |
| `schedule.intervalMinutes` | 起動後のスケジュール実行の間隔 (分)。Startupイベントがいつ実行されるかには影響しない                                                       |
| `tasks[0].command`         | バイナリ名。パスや拡張子は含めません。                                                                                   |
| `tasks[0].executablePath`  | エンドポイント上のバイナリのフルパス                                                                                    |
| `tasks[0].arguments`       | バイナリが受け付けるフラグ。`{KeeperApiBaseUrl}` は残します。                                                             |
| `tasks[0].timeoutSeconds`  | 1回あたりの最大実行時間。Startup実行と各インターバル実行は、それぞれこの上限を独立して使う                                                     |

## 動作の要点 <a href="#how-this-works" id="how-this-works"></a>

スケジュールのみのパターンからの変更点は、`"events": [{ "eventType": "Startup" }]` を追加するだけです。それ以外は同じです。

エージェント起動時にジョブを読み込んだうえで、Startupを宣言したジョブではStartupイベントが実行されます。Startup実行とインターバルタイマーは独立しており、インターバルはStartup実行の完了を待ってから数え始めるわけではありません。そのため、ツールの実行時間がインターバル境界に近い場合、Startup実行がまだ進行中に最初のインターバル実行が始まることがあります。同時実行に耐えるようツールを設計するか、`intervalMinutes` を最悪実行時間より十分長く設定してください。

Startupイベントはエージェント起動ごとに1回だけです。ジョブの再読み込みやポリシー更新だけでは再実行されず、エージェントサービス自体が再起動した場合に限り再度実行されます。

KeeperLoggerはStartupジョブより前に準備が整います。ログ基盤の初期化が完了してからStartupイベントが送られるため、Startup実行の早い段階のログも失われません。

### デプロイ前に <a href="#before-you-deploy" id="before-you-deploy"></a>

1. ジョブを登録する前に、エンドポイントの `executablePath` にバイナリをデプロイする。
2. 環境でStartup実行が安全か検討する。大規模フリートでは、エージェント再起動のたびに全エンドポイントで同時実行が走ります。連携先でリソース競合が起きる場合は、[ジョブ: スケジュールのみ](/keeperpam/jp/endpoint-privilege-manager/integrations/examples/job-schedule-only.md)を使い、最初の実行までの遅延を受け入れる。
3. ファイル名は `id` と一致させる。

## デプロイ <a href="#deploy" id="deploy"></a>

保存前に検証します。

```powershell
Invoke-RestMethod -Method Post `
  -Uri "https://127.0.0.1:6889/api/Jobs/validate" `
  -ContentType "application/json" `
  -Certificate $adminClientCert `
  -Body (Get-Content -Raw .\my-tool.json)
```

ジョブを作成します。

```powershell
Invoke-RestMethod -Method Post `
  -Uri "https://127.0.0.1:6889/api/Jobs" `
  -ContentType "application/json" `
  -Certificate $adminClientCert `
  -Body (Get-Content -Raw .\my-tool.json)
```

Startupの挙動を確認するには、エージェントサービスを再起動し、すぐに実行が記録されることを確認します。

```powershell
Invoke-RestMethod -Method Get `
  -Uri "https://127.0.0.1:6889/api/Jobs/my-tool" `
  -Certificate $adminClientCert
```


---

# 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://docs.keeper.io/keeperpam/jp/endpoint-privilege-manager/integrations/examples/job-schedule-and-startup.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.
