

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在队列环境中设置路径
<a name="set-the-path"></a>

使用 OpenJD 环境在环境中提供新命令。首先，创建一个包含脚本文件的目录，然后将该目录添加到`PATH`环境变量中，这样脚本中的可执行文件就可以运行它们，而无需每次都指定目录路径。环境定义中的变量列表不提供修改变量的方法，因此您可以通过运行脚本来完成此操作。在脚本设置并修改之后`PATH`，它会使用命令将变量导出到 OpenJD 运行时。`echo "openjd_env: PATH=$PATH"`

## 先决条件
<a name="set-prerequisites"></a>

 执行以下步骤，[使用来自 Deadline Cloud 示例 github 存储库的环境变量运行示例作业包](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/job_env_vars/template.yaml)。

1.  如果您没有包含队列和关联的 Linux 队列的 Deadline Cloud 场，请按照 De [adline Cloud 控制台](https://console.aws.amazon.com/deadlinecloud/home)中的指导性入门体验创建具有默认设置的群组。

1.  如果您的工作站上没有 Deadline Cloud CLI 和 Deadline Cloud 监控器，请按照用户指南中[设置 Deadline Cloud 提交者](https://docs.aws.amazon.com/deadline-cloud/latest/userguide/submitter.html)中的步骤进行操作。

1.  用于克隆 D `git` e [adline Cloud 示例 GitHub存储库](https://github.com/aws-deadline/deadline-cloud-samples)。

   ```
   git clone https://github.com/aws-deadline/deadline-cloud-samples.git
    Cloning into 'deadline-cloud-samples'...
    ...
   cd deadline-cloud-samples/job_bundles
   ```

## 运行路径示例
<a name="path-run-sample"></a>

1.  使用 Deadline Cloud CLI 提交`job_env_with_new_command`示例。

   ```
    $ deadline bundle submit job_env_with_new_command
    Submitting to Queue: MySampleQueue
    ...
   ```

1.  在 Deadline Cloud 监控器中，您将看到新作业并可以监控其进度。一旦与Linux队列关联的队列有工作人员可以运行作业的任务，作业将在几秒钟内完成。选择任务，然后在任务面板的右上角菜单中选择 “**查看日志**” 选项。

    右边是两个会话操作：“**启动” RandomSleepCommand 和 “****任务运行”**。窗口中央的日志查看器对应于右侧选定的会话操作。

## 将会话操作与其定义进行比较
<a name="path-view-logs"></a>

在本节中，您将使用 Deadline Cloud 监视器将会话操作与作业模板中定义的会话操作进行比较。它延续了上一节。

在文本编辑器中打开 [job\_env\_with\_new\_command/template.yaml 文件。](https://github.com/aws-deadline/deadline-cloud-samples/tree/mainline/job_bundles/job_env_with_new_command/template.yaml)将会话操作与作业模板中的定义位置进行比较。

1.  在 Deadline Cloud 监视器中选择 “**启动 RandomSleepCommand**会话” 操作。您将看到如下所示的日志输出。

   ```
    2024/07/16 17:25:32-07:00
    2024/07/16 17:25:32-07:00 ==============================================
    2024/07/16 17:25:32-07:00 --------- Entering Environment: RandomSleepCommand
    2024/07/16 17:25:32-07:00 ==============================================
    2024/07/16 17:25:32-07:00 ----------------------------------------------
    2024/07/16 17:25:32-07:00 Phase: Setup
    2024/07/16 17:25:32-07:00 ----------------------------------------------
    2024/07/16 17:25:32-07:00 Writing embedded files for Environment to disk.
    2024/07/16 17:25:32-07:00 Mapping: Env.File.Enter -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmpbt8j_c3f
    2024/07/16 17:25:32-07:00 Mapping: Env.File.SleepScript -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmperastlp4
    2024/07/16 17:25:32-07:00 Wrote: Enter -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmpbt8j_c3f
    2024/07/16 17:25:32-07:00 Wrote: SleepScript -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmperastlp4
    2024/07/16 17:25:32-07:00 ----------------------------------------------
    2024/07/16 17:25:32-07:00 Phase: Running action
    2024/07/16 17:25:32-07:00 ----------------------------------------------
    2024/07/16 17:25:32-07:00 Running command sudo -u job-user -i setsid -w /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/tmpbwrquq5u.sh
    2024/07/16 17:25:32-07:00 Command started as pid: 2205
    2024/07/16 17:25:32-07:00 Output:
    2024/07/16 17:25:33-07:00 openjd_env: PATH=/sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/bin:/opt/conda/condabin:/home/job-user/.local/bin:/home/job-user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/var/lib/snapd/snap/bin
    No newer logs at this moment.
   ```

    作业模板中的以下几行指定了此操作。

   ```
    jobEnvironments:
    - name: RandomSleepCommand
      description: Adds a command 'random-sleep' to the environment.
      script:
        actions:
          onEnter:
            command: bash
            args:
            - "{{Env.File.Enter}}"
        embeddedFiles:
        - name: Enter
          type: TEXT
          data: |
            #!/bin/env bash
            set -euo pipefail
   
            # Make a bin directory inside the session's working directory for providing new commands
            mkdir -p '{{Session.WorkingDirectory}}/bin'
   
            # If this bin directory is not already in the PATH, then add it
            if ! [[ ":$PATH:" == *':{{Session.WorkingDirectory}}/bin:'* ]]; then
              export "PATH={{Session.WorkingDirectory}}/bin:$PATH"
   
              # This message to Open Job Description exports the new PATH value to the environment
              echo "openjd_env: PATH=$PATH"
            fi
   
            # Copy the SleepScript embedded file into the bin directory
            cp '{{Env.File.SleepScript}}' '{{Session.WorkingDirectory}}/bin/random-sleep'
            chmod u+x '{{Session.WorkingDirectory}}/bin/random-sleep'
        - name: SleepScript
          type: TEXT
          runnable: true
          data: |
            ...
   ```

1.  在 Deadline Cloud 监视器中选择 “**启动 StepEnv**会话” 操作。您将看到如下所示的日志输出。

   ```
    2024/07/16 17:25:33-07:00
    2024/07/16 17:25:33-07:00 ==============================================
    2024/07/16 17:25:33-07:00 --------- Running Task
    2024/07/16 17:25:33-07:00 ==============================================
    2024/07/16 17:25:33-07:00 ----------------------------------------------
    2024/07/16 17:25:33-07:00 Phase: Setup
    2024/07/16 17:25:33-07:00 ----------------------------------------------
    2024/07/16 17:25:33-07:00 Writing embedded files for Task to disk.
    2024/07/16 17:25:33-07:00 Mapping: Task.File.Run -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmpdrwuehjf
    2024/07/16 17:25:33-07:00 Wrote: Run -> /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/embedded_filesf3tq_1os/tmpdrwuehjf
    2024/07/16 17:25:33-07:00 ----------------------------------------------
    2024/07/16 17:25:33-07:00 Phase: Running action
    2024/07/16 17:25:33-07:00 ----------------------------------------------
    2024/07/16 17:25:33-07:00 Running command sudo -u job-user -i setsid -w /sessions/session-ab132a51b9b54d5da22cbe839dd946baaw1c8hk5/tmpz81iaqfw.sh
    2024/07/16 17:25:33-07:00 Command started as pid: 2256
    2024/07/16 17:25:33-07:00 Output:
    2024/07/16 17:25:34-07:00 + random-sleep 12.5 27.5
    2024/07/16 17:26:00-07:00 Sleeping for duration 26.90
    2024/07/16 17:26:00-07:00 ----------------------------------------------
    2024/07/16 17:26:00-07:00 Uploading output files to Job Attachments
    2024/07/16 17:26:00-07:00 ----------------------------------------------
   ```

1.  作业模板中的以下几行指定了此操作。

   ```
    steps:
    - name: EnvWithCommand
      script:
        actions:
          onRun:
            command: bash
            args:
            - '{{Task.File.Run}}'
        embeddedFiles:
        - name: Run
          type: TEXT
          data: |
            set -xeuo pipefail
   
            # Run the script installed into PATH by the job environment
            random-sleep 12.5 27.5
      hostRequirements:
        attributes:
        - name: attr.worker.os.family
          anyOf:
          - linux
   ```