github.com/adityamillind98/nomad@v0.11.8/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-session
    59  $ nomad alloc exec eb17e557 /bin/bash
    60  root@eb17e557:/data# # now run any debugging commands inside container
    61  root@eb17e557:/data# # ps -ef
    62  ```
    63  
    64  To run a command and stream results without starting an interactive shell, you
    65  can pass the command and its arguments to exec directly:
    66  
    67  ```shell-session# run commands without starting an interactive session
    68  $ nomad alloc exec eb17e557 cat /etc/resolv.conf
    69  ...
    70  ```
    71  
    72  When passing command arguments to be evaluated in task, you may need to ensure
    73  that your host shell doesn't interpolate values before invoking `exec` command.
    74  For example, the following command would return the environment variable on
    75  operator shell rather than task containers:
    76  
    77  ```shell-session
    78  $ nomad alloc exec eb17e557 echo $NOMAD_ALLOC_ID # wrong
    79  ...
    80  ```
    81  
    82  Here, we must start a shell in task to interpolate `$NOMAD_ALLOC_ID`, and quote
    83  command or use the [heredoc syntax][heredoc]
    84  
    85  ```shell-session# by quoting argument
    86  $ nomad alloc exec eb17e557 /bin/sh -c 'echo $NOMAD_ALLOC_ID'
    87  eb17e557-443e-4c51-c049-5bba7a9850bc
    88  
    89  $ # by using heredoc and passing command in stdin
    90  $ nomad alloc exec eb17e557 /bin/sh <<'EOF'
    91  > echo $NOMAD_ALLOC_ID
    92  > EOF
    93  eb17e557-443e-4c51-c049-5bba7a9850bc
    94  ```
    95  
    96  This technique applies when aiming to run a shell pipeline without streaming
    97  intermediate command output across the network:
    98  
    99  ```shell-session# e.g. find top appearing lines in some output
   100  $ nomad alloc exec eb17e557 /bin/sh -c 'cat /output | sort | uniq -c | sort -rn | head -n 5'
   101  ...
   102  ```
   103  
   104  ## Using Job ID instead of Allocation ID
   105  
   106  Setting the `-job` flag causes a random allocation of the specified job to be
   107  selected.
   108  
   109  ```plaintext
   110  nomad alloc exec -job <job-id> <command> [<args>...]
   111  ```
   112  
   113  Choosing a specific allocation is useful for debugging issues with a specific
   114  instance of a service. For other operations using the `-job` flag may be more
   115  convenient than looking up an allocation ID to use.
   116  
   117  ## Disabling remote execution
   118  
   119  `alloc exec` is enabled by default to aid with debugging. Operators can disable
   120  the feature by setting [`disable_remote_exec` client config
   121  option][disable_remote_exec_flag] on all clients, or a subset of clients that
   122  run sensitive workloads.
   123  
   124  ## Exec targeting a specific task
   125  
   126  When trying to `alloc exec` for a job that has more than one task associated
   127  with it, you may want to target a specific task.
   128  
   129  ```shell-session
   130  # open a shell session in one of your allocation's tasks
   131  $ nomad alloc exec -i -t -task mytask a1827f93 /bin/bash
   132  a1827f93$
   133  ```
   134  
   135  [heredoc]: http://tldp.org/LDP/abs/html/here-docs.html
   136  [disable_remote_exec_flag]: /docs/configuration/client#disable_remote_exec