github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/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 When ACLs are enabled, this command requires a token with the `alloc-exec`, 32 `read-job`, and `list-jobs` capabilities for the allocation's namespace. If 33 the task driver does not have file system isolation (as with `raw_exec`), 34 this command requires the `alloc-node-exec`, `read-job`, and `list-jobs` 35 capabilities for the allocation's namespace. 36 37 ## General Options 38 39 @include 'general_options.mdx' 40 41 ## Exec Options 42 43 - `-task`: Sets the task to exec command in. 44 45 - `-job`: Use a random allocation from the specified job ID. 46 47 - `-i`: Pass stdin to the container, defaults to true. Pass `-i=false` to 48 disable explicitly. 49 50 - `-t`: Allocate a pseudo-tty, defaults to true if stdin is detected to be a tty 51 session. Pass `-t=false` to disable explicitly. 52 53 - `-e` <escape_char>: Sets the escape character for sessions with a pty 54 (default: '~'). The escape character is only recognized at the beginning of a 55 line. The escape character followed by a dot ('.') closes the connection. 56 Setting the character to 'none' disables any escapes and makes the session 57 fully transparent. 58 59 ## Examples 60 61 To start an interactive debugging session in a particular alloc, invoke exec 62 command with your desired shell available inside the task: 63 64 ```shell-session 65 $ nomad alloc exec eb17e557 /bin/bash 66 root@eb17e557:/data# # now run any debugging commands inside container 67 root@eb17e557:/data# # ps -ef 68 ``` 69 70 To run a command and stream results without starting an interactive shell, you 71 can pass the command and its arguments to exec directly: 72 73 ```shell-session# run commands without starting an interactive session 74 $ nomad alloc exec eb17e557 cat /etc/resolv.conf 75 ... 76 ``` 77 78 When passing command arguments to be evaluated in task, you may need to ensure 79 that your host shell doesn't interpolate values before invoking `exec` command. 80 For example, the following command would return the environment variable on 81 operator shell rather than task containers: 82 83 ```shell-session 84 $ nomad alloc exec eb17e557 echo $NOMAD_ALLOC_ID # wrong 85 ... 86 ``` 87 88 Here, we must start a shell in task to interpolate `$NOMAD_ALLOC_ID`, and quote 89 command or use the [heredoc syntax][heredoc] 90 91 ```shell-session# by quoting argument 92 $ nomad alloc exec eb17e557 /bin/sh -c 'echo $NOMAD_ALLOC_ID' 93 eb17e557-443e-4c51-c049-5bba7a9850bc 94 95 $ # by using heredoc and passing command in stdin 96 $ nomad alloc exec eb17e557 /bin/sh <<'EOF' 97 > echo $NOMAD_ALLOC_ID 98 > EOF 99 eb17e557-443e-4c51-c049-5bba7a9850bc 100 ``` 101 102 This technique applies when aiming to run a shell pipeline without streaming 103 intermediate command output across the network: 104 105 ```shell-session# e.g. find top appearing lines in some output 106 $ nomad alloc exec eb17e557 /bin/sh -c 'cat /output | sort | uniq -c | sort -rn | head -n 5' 107 ... 108 ``` 109 110 ## Using Job ID instead of Allocation ID 111 112 Setting the `-job` flag causes a random allocation of the specified job to be 113 selected. 114 115 ```plaintext 116 nomad alloc exec -job <job-id> <command> [<args>...] 117 ``` 118 119 Choosing a specific allocation is useful for debugging issues with a specific 120 instance of a service. For other operations using the `-job` flag may be more 121 convenient than looking up an allocation ID to use. 122 123 ## Disabling remote execution 124 125 `alloc exec` is enabled by default to aid with debugging. Operators can disable 126 the feature by setting [`disable_remote_exec` client config 127 option][disable_remote_exec_flag] on all clients, or a subset of clients that 128 run sensitive workloads. 129 130 ## Exec targeting a specific task 131 132 When trying to `alloc exec` for a job that has more than one task associated 133 with it, you may want to target a specific task. 134 135 ```shell-session 136 # open a shell session in one of your allocation's tasks 137 $ nomad alloc exec -i -t -task mytask a1827f93 /bin/bash 138 a1827f93$ 139 ``` 140 141 [heredoc]: http://tldp.org/LDP/abs/html/here-docs.html 142 [disable_remote_exec_flag]: /docs/configuration/client#disable_remote_exec