github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/website/pages/docs/commands/alloc/exec.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: 'Commands: alloc exec'
     4  sidebar_title: exec
     5  description: |
     6    Runs a command in a container.
     7  ---
     8  
     9  # Command: alloc exec
    10  
    11  **Alias: `nomad exec`**
    12  
    13  The `alloc exec` command runs a command in a running allocation.
    14  
    15  ## Usage
    16  
    17  ```plaintext
    18  nomad alloc exec [options] <allocation> <command> [<args>...]
    19  ```
    20  
    21  The nomad exec command can be use to run commands inside a running task/allocation.
    22  
    23  Use cases are for inspecting container state, debugging a failed application
    24  without needing ssh access into the node that's running the allocation.
    25  
    26  This command executes the command in the given task in the allocation. If the
    27  allocation is only running a single task, the task name can be omitted.
    28  Optionally, the `-job` option may be used in which case a random allocation from
    29  the given job will be chosen.
    30  
    31  ## General Options
    32  
    33  @include 'general_options.mdx'
    34  
    35  ## Exec Options
    36  
    37  - `-task`: Sets the task to exec command in.
    38  
    39  - `-job`: Use a random allocation from the specified job ID.
    40  
    41  - `-i`: Pass stdin to the container, defaults to true. Pass `-i=false` to
    42    disable explicitly.
    43  
    44  - `-t`: Allocate a pseudo-tty, defaults to true if stdin is detected to be a tty
    45    session. Pass `-t=false` to disable explicitly.
    46  
    47  - `-e` <escape_char>: Sets the escape character for sessions with a pty
    48    (default: '~'). The escape character is only recognized at the beginning of a
    49    line. The escape character followed by a dot ('.') closes the connection.
    50    Setting the character to 'none' disables any escapes and makes the session
    51    fully transparent.
    52  
    53  ## Examples
    54  
    55  To start an interactive debugging session in a particular alloc, invoke exec
    56  command with your desired shell available inside the task:
    57  
    58  ```shell-sessionnomad alloc exec eb17e557 /bin/bash
    59  root@eb17e557:/data# # now run any debugging commands inside container
    60  root@eb17e557:/data# # ps -ef
    61  ```
    62  
    63  To run a command and stream results without starting an interactive shell, you
    64  can pass the command and its arguments to exec directly:
    65  
    66  ```shell-session# run commands without starting an interactive session
    67  $ nomad alloc exec eb17e557 cat /etc/resolv.conf
    68  ...
    69  ```
    70  
    71  When passing command arguments to be evaluated in task, you may need to ensure
    72  that your host shell doesn't interpolate values before invoking `exec` command.
    73  For example, the following command would return the environment variable on
    74  operator shell rather than task containers:
    75  
    76  ```shell-sessionnomad alloc exec eb17e557 echo $NOMAD_ALLOC_ID # wrong
    77  ...
    78  ```
    79  
    80  Here, we must start a shell in task to interpolate `$NOMAD_ALLOC_ID`, and quote
    81  command or use the [heredoc syntax][heredoc]
    82  
    83  ```shell-session# by quoting argument
    84  $ nomad alloc exec eb17e557 /bin/sh -c 'echo $NOMAD_ALLOC_ID'
    85  eb17e557-443e-4c51-c049-5bba7a9850bc
    86  
    87  $ # by using heredoc and passing command in stdin
    88  $ nomad alloc exec eb17e557 /bin/sh <<'EOF'
    89  > echo $NOMAD_ALLOC_ID
    90  > EOF
    91  eb17e557-443e-4c51-c049-5bba7a9850bc
    92  ```
    93  
    94  This technique applies when aiming to run a shell pipeline without streaming
    95  intermediate command output across the network:
    96  
    97  ```shell-session# e.g. find top appearing lines in some output
    98  $ nomad alloc exec eb17e557 /bin/sh -c 'cat /output | sort | uniq -c | sort -rn | head -n 5'
    99  ...
   100  ```
   101  
   102  ## Using Job ID instead of Allocation ID
   103  
   104  Setting the `-job` flag causes a random allocation of the specified job to be
   105  selected.
   106  
   107  ```plaintext
   108  nomad alloc exec -job <job-id> <command> [<args>...]
   109  ```
   110  
   111  Choosing a specific allocation is useful for debugging issues with a specific
   112  instance of a service. For other operations using the `-job` flag may be more
   113  convenient than looking up an allocation ID to use.
   114  
   115  ## Disabling remote execution
   116  
   117  `alloc exec` is enabled by default to aid with debugging. Operators can disable
   118  the feature by setting [`disable_remote_exec` client config
   119  option][disable_remote_exec_flag] on all clients, or a subset of clients that
   120  run sensitive workloads.
   121  
   122  ## Exec targeting a specific task
   123  
   124  When trying to `alloc exec` for a job that has more than one task associated
   125  with it, you may want to target a specific task.
   126  
   127  ```shell
   128  # open a shell session in one of your allocation's tasks
   129  $ nomad alloc exec -i -t -task mytask a1827f93 /bin/bash
   130  a1827f93$
   131  ```
   132  
   133  [heredoc]: http://tldp.org/LDP/abs/html/here-docs.html
   134  [disable_remote_exec_flag]: /docs/configuration/client#disable_remote_exec