anchor.diff:  page_title: "Commands: alloc exec"
   sidebar_current: "docs-commands-alloc-exec"
   description: >
  -Stream the exec of a task.
  +  Runs a command in a container.
   ---
   
   # Command: alloc exec
  
positive.code: 
  ```
  nomad alloc exec [options] <allocation> <command> [<args>...]
      ```
  
  
  The nomad exec command can be use to run commands inside a running task/allocation.
  
negative.code: 
  ## Examples
  
  To start an interactive debugging session in a particular alloc, invoke exec
  command with your desired shell available inside the task:
  
  ```
  $ 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:
  
  ```
  $ # run commands without starting an interactive session
  $ nomad alloc exec eb17e557 cat /etc/resolv.conf
  ```
  
  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:
  
  ```
  $ nomad alloc exec eb17e557 echo $NOMAD_ALLOC_ID # wrong
  ```
  
  Here, we must start a shell in task to interpolate `$NOMAD_ALLOC_ID`, and quote
  command or use the [heredoc syntax][heredoc]
  
  ```
  $ # 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:
  
  ```
  $ # 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'
  ```
  
  [heredoc]: http://tldp.org/LDP/abs/html/here-docs.html
  
  ## Using Job ID instead of Allocation ID
  
  