2019-05-13 00:03:31 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Commands: alloc exec"
|
|
|
|
sidebar_current: "docs-commands-alloc-exec"
|
|
|
|
description: >
|
2019-05-20 14:06:15 +00:00
|
|
|
Runs a command in a container.
|
2019-05-13 00:03:31 +00:00
|
|
|
---
|
|
|
|
|
2019-06-05 15:09:22 +00:00
|
|
|
# Command: alloc exec
|
2019-10-22 13:44:00 +00:00
|
|
|
|
2019-05-13 00:03:31 +00:00
|
|
|
**Alias: `nomad exec`**
|
|
|
|
|
|
|
|
The `alloc exec` command runs a command in a running allocation.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```plaintext
|
2019-05-13 00:03:31 +00:00
|
|
|
nomad alloc exec [options] <allocation> <command> [<args>...]
|
2019-05-20 14:06:15 +00:00
|
|
|
```
|
2019-05-17 17:39:09 +00:00
|
|
|
|
|
|
|
The nomad exec command can be use to run commands inside a running task/allocation.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-05-17 17:39:09 +00:00
|
|
|
Use cases are for inspecting container state, debugging a failed application
|
|
|
|
without needing ssh access into the node that's running the allocation.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-05-20 14:06:15 +00:00
|
|
|
This command executes the command in the given task in the allocation. If the
|
2019-05-13 00:03:31 +00:00
|
|
|
allocation is only running a single task, the task name can be omitted.
|
|
|
|
Optionally, the `-job` option may be used in which case a random allocation from
|
|
|
|
the given job will be chosen.
|
2019-05-20 14:06:15 +00:00
|
|
|
|
2019-05-13 00:03:31 +00:00
|
|
|
## General Options
|
|
|
|
|
|
|
|
<%= partial "docs/commands/_general_options" %>
|
|
|
|
|
|
|
|
## Exec Options
|
|
|
|
|
2019-11-19 17:20:07 +00:00
|
|
|
- `-task`: Sets the task to exec command in.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
- `-job`: Use a random allocation from the specified job ID.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
- `-i`: Pass stdin to the container, defaults to true. Pass `-i=false` to
|
|
|
|
disable explicitly.
|
2019-05-17 17:39:09 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
- `-t`: Allocate a pseudo-tty, defaults to true if stdin is detected to be a tty
|
|
|
|
session. Pass `-t=false` to disable explicitly.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
- `-e` <escape_char>: Sets the escape character for sessions with a pty
|
|
|
|
(default: '~'). The escape character is only recognized at the beginning of a
|
|
|
|
line. The escape character followed by a dot ('.') closes the connection.
|
|
|
|
Setting the character to 'none' disables any escapes and makes the session
|
|
|
|
fully transparent.
|
2019-05-13 00:03:31 +00:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2019-05-20 14:06:15 +00:00
|
|
|
To start an interactive debugging session in a particular alloc, invoke exec
|
|
|
|
command with your desired shell available inside the task:
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-05-20 14:06:15 +00:00
|
|
|
$ nomad alloc exec eb17e557 /bin/bash
|
|
|
|
root@eb17e557:/data# # now run any debugging commands inside container
|
|
|
|
root@eb17e557:/data# # ps -ef
|
|
|
|
```
|
|
|
|
|
|
|
|
To run a command and stream results without starting an interactive shell, you
|
|
|
|
can pass the command and its arguments to exec directly:
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-05-17 17:39:09 +00:00
|
|
|
$ # run commands without starting an interactive session
|
|
|
|
$ nomad alloc exec eb17e557 cat /etc/resolv.conf
|
2019-10-22 13:44:00 +00:00
|
|
|
...
|
2019-05-20 14:06:15 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
When passing command arguments to be evaluated in task, you may need to ensure
|
|
|
|
that your host shell doesn't interpolate values before invoking `exec` command.
|
|
|
|
For example, the following command would return the environment variable on
|
|
|
|
operator shell rather than task containers:
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-05-20 14:06:15 +00:00
|
|
|
$ nomad alloc exec eb17e557 echo $NOMAD_ALLOC_ID # wrong
|
2019-10-22 13:44:00 +00:00
|
|
|
...
|
2019-05-20 14:06:15 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Here, we must start a shell in task to interpolate `$NOMAD_ALLOC_ID`, and quote
|
|
|
|
command or use the [heredoc syntax][heredoc]
|
2019-05-13 00:03:31 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-05-20 14:06:15 +00:00
|
|
|
$ # by quoting argument
|
|
|
|
$ nomad alloc exec eb17e557 /bin/sh -c 'echo $NOMAD_ALLOC_ID'
|
|
|
|
eb17e557-443e-4c51-c049-5bba7a9850bc
|
|
|
|
|
|
|
|
$ # by using heredoc and passing command in stdin
|
|
|
|
$ nomad alloc exec eb17e557 /bin/sh <<'EOF'
|
|
|
|
> echo $NOMAD_ALLOC_ID
|
|
|
|
> EOF
|
|
|
|
eb17e557-443e-4c51-c049-5bba7a9850bc
|
|
|
|
```
|
|
|
|
|
|
|
|
This technique applies when aiming to run a shell pipeline without streaming
|
|
|
|
intermediate command output across the network:
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-05-20 14:06:15 +00:00
|
|
|
$ # e.g. find top appearing lines in some output
|
|
|
|
$ nomad alloc exec eb17e557 /bin/sh -c 'cat /output | sort | uniq -c | sort -rn | head -n 5'
|
2019-10-22 13:44:00 +00:00
|
|
|
...
|
2019-05-20 14:06:15 +00:00
|
|
|
```
|
|
|
|
|
2019-05-13 00:03:31 +00:00
|
|
|
## Using Job ID instead of Allocation ID
|
|
|
|
|
|
|
|
Setting the `-job` flag causes a random allocation of the specified job to be
|
|
|
|
selected.
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```plaintext
|
2019-05-13 00:03:31 +00:00
|
|
|
nomad alloc exec -job <job-id> <command> [<args>...]
|
|
|
|
```
|
|
|
|
|
2019-05-17 17:39:09 +00:00
|
|
|
Choosing a specific allocation is useful for debugging issues with a specific
|
|
|
|
instance of a service. For other operations using the `-job` flag may be more
|
|
|
|
convenient than looking up an allocation ID to use.
|
2019-06-04 18:37:56 +00:00
|
|
|
|
|
|
|
## Disabling remote execution
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
`alloc exec` is enabled by default to aid with debugging. Operators can disable
|
|
|
|
the feature by setting [`disable_remote_exec` client config
|
|
|
|
option][disable_remote_exec_flag] on all clients, or a subset of clients that
|
|
|
|
run sensitive workloads.
|
2019-08-09 14:16:45 +00:00
|
|
|
|
|
|
|
## Exec targeting a specific task
|
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
When trying to `alloc exec` for a job that has more than one task associated
|
|
|
|
with it, you may want to target a specific task.
|
2019-08-09 14:16:45 +00:00
|
|
|
|
2019-10-22 13:44:00 +00:00
|
|
|
```shell
|
2019-08-09 14:16:45 +00:00
|
|
|
# open a shell session in one of your allocation's tasks
|
|
|
|
$ nomad alloc exec -i -t -task mytask a1827f93 /bin/bash
|
2019-10-22 13:44:00 +00:00
|
|
|
a1827f93$
|
2019-08-09 14:16:45 +00:00
|
|
|
```
|
2019-10-22 13:44:00 +00:00
|
|
|
|
|
|
|
[heredoc]: http://tldp.org/LDP/abs/html/here-docs.html
|
|
|
|
[disable_remote_exec_flag]: /docs/configuration/client.html#disable_remote_exec
|