

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 自動下載
<a name="auto-downloads"></a>

截止日期 CLI 提供命令，以下載自上次執行相同命令後佇列中所有任務的輸出。您可以將此設定為 Cron 任務或排程任務，以重複執行。此組態會設定持續自動下載輸出。

在設定自動下載之前，請遵循[任務附件的儲存設定檔](https://docs.aws.amazon.com/deadline-cloud/latest/userguide/storage-profile.html)中的步驟，設定資產資料的所有路徑以進行上傳和下載。如果任務使用不在其儲存設定檔中的輸出路徑，則自動下載會略過下載該輸出並列印警告訊息，以摘要其未下載的檔案。同樣地，如果提交的任務沒有儲存描述檔，則自動下載會略過該任務並列印警告訊息。根據預設，截止日期雲端提交者會顯示儲存設定檔以外路徑的警告訊息，以協助確保正確的組態。

## 設定 AWS 登入資料
<a name="credentials"></a>

自動下載使用截止日期 CLI 來持續下載任務輸出。若要驗證這些下載，您需要長期 IAM 登入資料。截止日期的雲端監視器登入資料會過期，因此您無法將其用於此目的。

 請依照下列步驟設定長期登入資料。

**重要**  
請注意下列警告：  
**請勿使用**您帳戶的根登入資料來存取 AWS 資源。這些登入資料可讓未管制的帳戶存取和很難撤銷這些帳戶。
**請勿**在應用程式檔案中放置常值存取金鑰或憑證資訊。如果您不小心這麼做了，則會有暴露您登入資料的風險，例如，當您上傳專案到公有儲存庫時。
**請勿在**您的專案區域中放入包含憑證的檔案。
保護您的存取金鑰。請勿將您的存取金鑰提供給未經授權的當事方，即便是協助[尋找您的帳戶識別符](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-identifiers.html)也不妥。如果這麼做，就可能會讓他人能夠永久存取您的帳戶。
請注意，存放在共用 AWS 登入資料檔案中的任何登入資料都會以純文字形式儲存。
如需詳細資訊，請參閱《 [*AWS 一般參考*》中的管理 AWS 存取金鑰的最佳實務。](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#securing_access-keys)

**建立 IAM 使用者**

1. 前往 [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 開啟 IAM 主控台。

1. 在導覽窗格中選取**使用者**，然後選取**建立使用者**。

1. 將使用者命名為 **deadline-output-downloader**。清除**提供使用者存取權 AWS 管理主控台**的核取方塊，然後選擇**下一步**。

1. 選擇**直接連接政策**。

1. 選擇**建立政策**以建立具有最低必要許可的自訂政策。

1. 在 JSON 編輯器中，指定下列許可：

------
#### [ JSON ]

****  

   ```
   {
                      "Version":"2012-10-17",		 	 	 
                      "Statement": [
                          {
                              "Sid": "DeadlineCloudOutputDownload",
                              "Effect": "Allow",
                              "Action": [
                                  "deadline:AssumeQueueRoleForUser",
                                  "deadline:ListQueueEnvironments",
                                  "deadline:ListSessions",
                                  "deadline:ListSessionActions",
                                  "deadline:SearchJobs",
                                  "deadline:GetJob",
                                  "deadline:GetQueue",
                                  "deadline:GetStorageProfileForQueue"
                              ],
                              "Resource": "*"
                          }
                        ]
                     }
   ```

------

1. 命名政策**DeadlineCloudOutputDownloadPolicy**，然後選擇**建立政策**。

1. 返回使用者建立頁面，重新整理政策清單，然後選取您剛建立的 **DeadlineCloudOutputDownloadPolicy**，然後選擇**下一步**。

1. 檢閱使用者詳細資訊，然後選擇**建立使用者**。

**建立存取金鑰**

1. 從使用者詳細資訊頁面，選取**安全登入**資料索引標籤。在**存取金鑰**區段中，選擇**建立存取金鑰**。

1. 表示您想要使用其他 的金鑰，然後選擇**下一步**，然後選擇**建立存取金鑰**。

1. 在**擷取存取金鑰**頁面上，選擇**顯示**以顯示使用者私密存取金鑰的值。您可以複製憑證或下載 .csv 檔案。

**存放使用者存取金鑰**
+ 將使用者存取金鑰存放在系統中的 AWS 登入資料檔案中：
  + 在 上Linux，檔案位於 `~/.aws/credentials`
  + 在 上Windows，檔案位於 `%USERPROFILE\.aws\credentials`

  取代下列金鑰：

  ```
  [deadline-downloader]
  aws_access_key_id={{ACCESS_KEY_ID}}
  aws_secret_access_key={{SECRET_ACCESS_KEY}}
  region={{YOUR_AWS_REGION}}
  ```

**重要**  
當您不再需要此 IAM 使用者時，建議您將其移除，以符合[AWS 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#lock-away-credentials)。我們建議您要求人類使用者在存取 [AWS IAM Identity Center](https://docs.aws.amazon.com/singlesignon/latest/userguide/getting-started.html)時透過 使用暫時登入資料 AWS。

## 先決條件
<a name="auto-downloads-prerequisites"></a>

在建立 cron 任務或排程任務以進行自動下載之前，請完成下列步驟。

1. 如果您尚未安裝 [Python](https://www.python.org/)，請安裝 Python。

1. 執行下列動作來安裝截止日期 CLI：

   ```
   python -m pip install deadline
   ```

1. 使用以下命令確認截止日期 CLI 的版本為 0.52.1 或更新版本。

   ```
   $ deadline --version
   deadline, version 0.52.1
   ```

## 測試輸出下載命令
<a name="test-output-command"></a>

**驗證命令是否可在您的環境中運作**

1. 取得截止日期的路徑

------
#### [ Linux and macOS ]

   ```
   $ which deadline
   ```

------
#### [ Windows ]

   ```
   C:\> where deadline
   ```

------
#### [ PowerShell ]

   ```
   PS C:\> Get-Command deadline
   ```

------

1. 執行 sync-output 命令以引導。

   ```
     /path/to/deadline queue sync-output \
     --profile deadline-downloader \
     --farm-id YOUR_FARM_ID \
     --queue-id YOUR_QUEUE_ID \
     --storage-profile-id YOUR_PROFILE_ID \
     --checkpoint-dir /path/to/checkpoint/directory \
   ```

1. 只有在您的下載機器與提交機器相同時，才需要執行此步驟。將`--storage-profile-id YOUR_PROFILE_ID \`上述 取代為 `--ignore-storage-profiles`。

1. 提交測試任務。

   1. 從 GitHub 下載 .zip 檔案。

      1. 開啟 [deadline-cloud-samples GitHub 儲存庫](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline)。

      1. 選擇**程式碼**，然後從下拉式功能表中選取**下載 ZIP**。

      1. 將下載的封存解壓縮至本機目錄。

   1. 執行

      ```
       cd /path/to/unzipped/deadline-cloud-samples-mainline/job_bundles/job_attachments_devguide_output
      ```

   1. 執行

      ```
      deadline bundle submit .
      ```

      1. 如果您沒有預設的截止日期組態設定，您可能需要在命令列中提供下列項目。

        ```
        --farm-id {{YOUR-FARM-ID}} --queue-id {{YOUR-QUEUE-ID}}
        ```

   1. 等待任務完成，再前往下一個步驟。

1. 再次執行 sync-output 命令。

   ```
    /path/to/deadline queue sync-output \
     --profile deadline-downloader \
     --farm-id YOUR_FARM_ID \
     --queue-id YOUR_QUEUE_ID \
     --storage-profile-id YOUR_PROFILE_ID \
     --checkpoint-dir /path/to/checkpoint/directory
   ```

1. 請確認下列內容：
   + 您的測試任務輸出會出現在目的地目錄中。
   + 檢查點檔案會在您指定的檢查點目錄中建立。

## 設定排定的下載
<a name="set-up-scheduled-downloads"></a>

選取作業系統的索引標籤，了解如何設定每 5 分鐘自動下載一次。

------
#### [ Linux ]

1. **驗證截止日期 CLI 安裝**

   取得截止日期可執行檔的確切路徑：

   ```
   $ which deadline
   ```

   請注意此路徑 （例如 `/opt/homebrew/bin/deadline`) 可用於 plist 檔案。

1. **建立檢查點目錄**

   建立存放檢查點檔案的目錄。確保您的使用者執行 命令的適當許可。

   ```
   $ mkdir -p /path/to/checkpoint/directory
   ```

1. **建立日誌目錄**

   建立 cron 任務日誌的目錄：

   ```
   $ mkdir -p /path/to/logs
   ```

   考慮使用 https://www.redhat.com/en/blog/setting-logrotate 在日誌檔案上設定日誌輪換

1. **檢查目前的 Crontab**

   檢視您目前的 crontab 以查看現有的任務：

   ```
   $ crontab -l
   ```

1. **編輯 Crontab**

   開啟您的 crontab 檔案以進行編輯：

   ```
   $ crontab -e
   ```

   如果這是您第一次使用，系統可能會提示您選擇編輯器 (nano、vim 等）。

1. **新增 Cron 任務項目**

   每 5 分鐘新增以下行來執行任務 （將路徑取代為步驟 1 和 2 的實際值）：

   ```
   */5 * * * * /path/to/deadline queue sync-output --profile deadline-downloader --farm-id YOUR_FARM_ID --queue-id YOUR_QUEUE_ID --storage-profile-id YOUR_PROFILE_ID --checkpoint-dir /path/to/checkpoint/directory >> /path/to/logs/deadline_sync.log 2>&1
   ```

1. **驗證 Cron 任務安裝**

   儲存並結束編輯器之後，請確認已新增 Cron 任務：

   ```
   $ crontab -l
   ```

   您應該會看到您的新任務已列出。

1. **檢查 Cron Service 狀態**

   確保 cron 服務正在執行：

   ```
   # For systemd systems (most modern Linux distributions)
   $ sudo systemctl status cron
   # or
   $ sudo systemctl status crond
   
   # For older systems
   $ sudo service cron status
   ```

   如果未執行，請啟動它：

   ```
   $ sudo systemctl start cron
   $ sudo systemctl enable cron  # Enable auto-start on boot
   ```

------
#### [ macOS ]

1. **驗證截止日期 CLI 安裝**

   取得截止日期可執行檔的確切路徑：

   ```
   $ which deadline
   ```

   請注意此路徑 （例如 `/opt/homebrew/bin/deadline`) 可用於 plist 檔案。

1. **建立檢查點目錄和日誌目錄**

   建立存放檢查點檔案的目錄：

   ```
   $ mkdir -p /path/to/checkpoint/directory
   $ mkdir -p /path/to/logs
   ```

   考慮使用 https://formulae.brew.sh/formula/logrotate 在日誌檔案上設定日誌輪換

1. **建立 Plist 檔案**

   使用`~/Library/LaunchAgents/com.user.deadlinesync.plist`下列內容在 建立組態檔案 (`/path/to/deadline`以步驟 1 的實際路徑取代）：

   ```
   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
   <plist version="1.0">
   <dict>
       <key>Label</key>
       <string>com.user.deadlinesync</string>
       <key>ProgramArguments</key>
       <array>
           <string>/path/to/deadline</string>
           <string>queue</string>
           <string>sync-output</string>
           <string>--profile</string>
           <string>deadline-downloader</string>
           <string>--farm-id</string>
           <string>YOUR_FARM_ID</string>
           <string>--queue-id</string>
           <string>YOUR_QUEUE_ID</string>
           <string>--storage-profile-id</string>
           <string>YOUR STORAGE PROFILE ID</string>
           <string>--checkpoint-dir</string>
           <string>/path/to/checkpoint/dir</string>
       </array>
       <key>RunAtLoad</key>
       <true/>
       <key>UserName</key>
       <string>YOUR_USER_NAME</string>
       <key>StandardOutPath</key>
       <string>/path/to/logs/deadline_sync.log</string>
       <key>StartInterval</key>
       <integer>300</integer>
   </dict>
   </plist>
   ```

   `--ignore-storage-profiles` 如果您的下載機器與提交機器相同，請將`--storage-profile-id {{YOUR_PROFILE_ID}}`上述 取代為 。

1. **驗證清單檔案**

   驗證 plist 檔案的 XML 語法：

   ```
   $ plutil -lint ~/Library/LaunchAgents/com.user.deadlinesync.plist
   ```

   如果檔案有效，這應該會傳回 "OK"。

1. **檢查現有的啟動代理程式或啟動協助程式**

   檢查啟動代理程式是否已載入：

   ```
   $ launchctl list | grep deadlinesync
   OR
   $ sudo launchctl list | grep deadlinesync
   ```

   如果存在，請先卸載：

   ```
   $ launchctl bootout gui/$(id -u)/com.user.deadlinesync
   OR
   $ sudo launchctl bootout system/com.user.deadlinesync
   ```

1. **建立和引導**

   若要在使用者登入時執行此任務，請以 **LaunchAgent** 身分執行。若要執行此任務，而不讓使用者在每次執行機器時登入，請以 **LaunchDaemon** 執行它。

   1. 若要以 **LaunchAgent 身分執行：**

      1. 使用在 下建立的組態 `~/Library/LaunchAgents/com.user.deadlinesync.plist`

      1. 然後使用引導命令載入組態：

         ```
         $ launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.user.deadlinesync.plist
         ```

   1. 若要以 **LaunchDaemon** 執行：

      1. 執行下列動作以移動 Pilst 檔案並變更許可：

         ```
         $ sudo mv ~/Library/LaunchAgents/com.user.deadlinesync.plist /Library/LaunchDaemons/ 
         $ sudo chown root:wheel /Library/LaunchDaemons/com.user.deadlinesync.plist 
         $ sudo chmod 644 /Library/LaunchDaemons/com.user.deadlinesync.plist
         ```

      1. 使用現代引導命令載入啟動代理程式：

         ```
         $ sudo launchctl bootstrap system /Library/LaunchDaemons/com.user.deadlinesync.plist
         ```

1. **驗證狀態**

   如果您啟動 LaunchAgent，請執行下列動作以確認已載入：

   ```
   $ launchctl list | grep deadlinesync
   ```

   如果您啟動 LaunchDaemon，請執行下列動作來確認已載入：

   ```
   $ sudo launchctl list | grep deadlinesync
   ```

   輸出看起來應該像

   ```
   SOME_PID_NUMBER 0 com.user.deadlinesync
   ```

   如需詳細狀態資訊：

   ```
   $ launchctl print gui/$(id -u)/com.user.deadlinesync
   ```

   這會顯示目前狀態、程式引數、環境變數、執行間隔和執行歷史記錄。

------
#### [ Windows ]

**注意**  
使用這些指示建立的排程任務只有在使用者登入時才有效。  
若要在系統啟動時設定它，而不需要使用者登入，請參閱官方[Windows文件](https://learn.microsoft.com/en-us/windows/win32/taskschd/using-the-task-scheduler)。

對於以下所有步驟，請使用命令提示 - 以管理員身分執行：

1. **驗證截止日期 CLI 安裝**

   尋找截止日期可執行檔：

   ```
   C:\> where deadline
   ```

   記下用於任務的完整路徑 （例如 `C:\Program Files\Amazon\DeadlineCloud\deadline.exe`)。

1. **建立檢查點目錄**

   建立存放檢查點檔案的目錄：

   ```
   C:\> mkdir "path\to\checkpoint\directory"
   ```

1. **建立日誌目錄**

   建立任務日誌的目錄：

   ```
   C:\> mkdir "path\to\logs"
   ```

1. **建立批次檔案包裝函式**

   使用下列內容建立批次檔案：

   ```
   C:\> notepad C:\path\to\deadline_sync.bat
   ```

   ```
   YOUR_PATH_TO_DEADLINE.EXE queue sync-output --profile deadline-downloader --farm-id {{YOUR_FARM_ID}} --queue-id {{YOUR_QUEUE_ID}} --storage-profile-id {{YOUR_PROFILE_ID}} --checkpoint-dir path\to\checkpoint\checkpoints > path\to\logs\deadline.log 2>&1
   ```

1. **測試批次檔案**

   手動測試批次檔案：

   ```
   C:\> .\path\to\deadline_sync.bat
   ```

   檢查日誌檔案是否已建立：

   ```
   C:\> notepad path\to\logs\deadline_sync.log
   ```

1. **檢查任務排程器服務**

   確保任務排程器服務正在執行：

   ```
   C:\> sc query "Schedule"
   ```

   如果服務不存在，請嘗試其他名稱：

   ```
   C:\> sc query "TaskScheduler"
   C:\> sc query "Task Scheduler"
   ```

   如果未執行，請啟動它：

   ```
   C:\> sc start "Schedule"
   ```

1. **建立排程任務**

   建立任務以每 5 分鐘執行一次。

   ```
   C:\> schtasks /create /tn "DeadlineOutputSync" /tr "C:\path\to\deadline_sync.bat" /sc minute /mo 5
   ```

   命令明細：
   + `/tn` - 任務名稱
   + `/tr` - 要執行的任務 （您的批次檔案）
   + `/sc minute /mo 5` - 排程：每 5 分鐘

1. **驗證任務建立**

   檢查任務是否已成功建立：

   ```
   schtasks /query /tn "DeadlineOutputSync" /v /fo LIST
   ```

   尋找：
   + **要執行的任務**： 應該顯示您的批次檔案路徑
   + **下次執行時間**：應該會在 5 分鐘內顯示時間

1. **測試任務執行**

   手動執行任務以測試：

   ```
   schtasks /run /tn "DeadlineOutputSync"
   ```

   檢查任務狀態：

   ```
   schtasks /query /tn "DeadlineOutputSync"
   ```

------

**驗證設定**

若要驗證自動下載設定是否成功，請完成下列步驟。

1. 提交新的測試任務。

1. 等待一個排程器間隔完成，在此情況下為 5 分鐘。

1. 確認已自動下載新的輸出。

如果輸出未下載，請檢查程序日誌的故障診斷區段。

## 對自動下載進行故障診斷
<a name="auto-downloads-troubleshooting"></a>

如果您遇到自動下載的問題，請檢查下列項目：

### 儲存設定檔問題
<a name="storage-profile-issues"></a>
+ 日誌檔案中類似 `[Errno 2] No such file or directory`或 `[Errno 13] Permission denied`的錯誤可能與遺失或設定錯誤的儲存設定檔有關。
+ 如需下載機器與提交機器不同時如何設定儲存設定檔[https://docs.aws.amazon.com/deadline-cloud/latest/userguide/storage-profile-job-attachments.html](https://docs.aws.amazon.com/deadline-cloud/latest/userguide/storage-profile-job-attachments.html)的相關資訊，請參閱儲存設定檔。
+ 對於相同的機器下載，請嘗試 `--ignore-storage-profiles`旗標。

### 目錄許可
<a name="directory-permissions"></a>
+ 確保排程器服務使用者具有：
  + 對檢查點目錄的讀取/寫入存取權
  + 輸出目的地目錄的寫入存取權
+ 對於 Linux和 macOS，使用 `ls -la` 檢查許可。
+ 對於 Windows，請檢閱 屬性資料夾中的安全性設定。

### 檢查排程器日誌
<a name="checking-scheduler-logs"></a>

------
#### [ Linux ]

1. 檢查 cron 服務是否正在執行：

   ```
   # For systemd systems
   $ sudo systemctl status cron
   # or
   $ sudo systemctl status crond
   
   # Check if your user has cron job correctly configured
   $ crontab -l
   ```

1. 檢視 Cron 執行日誌：

   ```
   # Check system logs for cron activity (most common locations)
   $ sudo tail -f /var/log/syslog | grep CRON
   $ sudo tail -f /var/log/cron.log | grep deadline
   
   # View recent cron logs
   $ sudo journalctl -u cron -f
   $ sudo journalctl -u crond -f  # On some systems
   ```

1. 檢查您的特定 Cron 任務日誌：

   ```
   # View the log file specified in your cron job
   $ tail -100f /path/to/logs/deadline_sync.log
   ```

1. 在系統日誌中搜尋 Cron 任務執行：

   ```
   # Look for your specific cron job executions
   $ sudo grep "deadline.*incremental-output-download" /var/log/syslog
   
   # Check for cron job starts and completions
   $ sudo grep "$(whoami).*CMD.*deadline" /var/log/syslog
   ```

1. 檢查檢查點檔案更新：

   ```
   # List checkpoint files with timestamps
   $ ls -la /path/to/checkpoint/directory/
   
   # Check when checkpoint was last modified
   $ stat /path/to/checkpoint/directory/queue-*_download_checkpoint.json
   ```

1. 檢查日誌檔案：

   ```
   $ ls -la /path/to/log/deadline_sync.log
   ```

------
#### [ macOS ]

檢視啟動代理程式執行日誌：

1. 檢查啟動代理程式是否正在執行：

   ```
   $ sudo launchctl list | grep deadlinesync
   ```

   輸出顯示：`PID Status Label`(PID 將在目前未執行`-`時，這是間隔任務的正常情況）

1. 檢視詳細的啟動代理程式狀態：

   ```
   $ sudo launchctl print system/com.user.deadlinesync
   ```

   這會顯示執行歷史記錄、上次結束程式碼、執行次數和目前狀態。

1. 檢視啟動代理程式執行日誌：

   ```
   # View recent logs (last hour)
   log show --predicate 'subsystem contains "com.user.deadlinesync"' --last 1h
   
   # View logs from a specific time period
   log show --predicate 'subsystem contains "com.user.deadlinesync"' --start '2024-08-27 09:00:00'
   ```

1. 強制執行啟動代理程式以進行立即測試：

   ```
   $ sudo launchctl kickstart gui/$(id -u)/com.user.deadlinesync
   ```

   無論排程為何，這都會立即觸發任務，這對於測試很有用。

1. 檢查檢查點檔案更新：

   ```
   # List checkpoint files with timestamps
   $ ls -la /path/to/checkpoint/directory/
   ```

1. 檢查日誌檔案：

   ```
   $ ls -la /path/to/log/deadline_sync.log
   ```

------
#### [ Windows ]

1. 檢查任務排程器服務是否正在執行：

   ```
   C:\> sc query "Schedule"
   ```

   如果服務不存在，請嘗試其他名稱：

   ```
   C:\> sc query "TaskScheduler"
   C:\> sc query "Task Scheduler"
   ```

1. 檢視您的排程任務：

   ```
   C:> schtasks /query /tn "DeadlineOutputSync"
   ```

1. 檢查任務的日誌檔案：

   ```
   # View the log file created by your batch script
   C:> notepad C:\path\to\logs\deadline_sync.log
   ```

1. 檢查檢查點檔案更新：

   ```
   # List checkpoint files with timestamps
   C:> dir "C:\path\to\checkpoint\directory" /od
   ```

------