Skip to content

Latest commit

 

History

History
122 lines (104 loc) · 3.65 KB

File metadata and controls

122 lines (104 loc) · 3.65 KB

How to watch a command line by line

qDup sh commands have a watch list of commands that are invoked with each new line of output from the sh command. watch usually contains regex commands to match patterns in the output.

- sh: ./doSomething.sh
  watch:
  - regex: ERROR
    then:
    - ...

limitations

The doSomething.sh script is still running in the ssh connection while the watch commands are executing, therefore we cannot run commands that would need control of the ssh connection. The following commands cannot be used in watch:

  • sh - cannot write to the ssh connection while another sh is running

  • wait-for - cannot pause the script if an sh command is running

  • repeat-until- cannot block other watch commands

  • script - script can only be used if async: true is used to spawn a new ssh connection.

ok

- sh: ./doSomething.sh
  watch:
  - regex: ERROR
    then:
    - script:
        name: in-case-of-error
        async: true

not ok

- sh: ./doSomething.sh
  watch:
  - regex: ERROR
    then:
    - script: in-case-of-error

If you want to run some of the above commands in parallel to doSomething.sh then we recommend creating a second script and starting that script before doSomething.sh and sending a signal when doSomething.sh finishes.

scripts:
  alsoDoThis:
  - sh: ./this.sh
  - repeat-until: doSomething-done
    then:
    - sh: ...
  doSomething:
  - script:
      name: alsoDoThis
      async: true
  - sh: doSomething.sh
  - signal: doSomething-done

when to use watch

The watch section is best used for actions that cannot wait for the sh command to finish. This includes:

  • interrupting commands that will not end using ctrlC

- sh: tail -f /var/log/messages
  watch:
  - regex: FOO
    then:
    - ctrlC
  timer:
    10m: #max wait time
    - ctlrC-
  • signaling other scripts when conditions are ready

- sh: doSomething.sh
  watch:
  - regex: ready
    then:
    - signal: something_ready
  • starting other scripts when conditions are ready

- sh: doSomething.sh
  watch:
  - regex: ERROR
    then:
    - script:
        name: collect-error-data
        async: true

Note: some performance tests are sensitive to the overhead of establishing a new ssh connection. Use the signaling method to have the script start with a wait-for and only continue if signaled.

watching for interaction

Some sh commands require interaction. For example using subscription-manager asks for a password. There are two ways interact with a command.

watch, regex, send-text

The first option is using the watch context with a regex and the send-text command.

- sh: subscription-manager register --username=me@domain.com
  watch:
  - regex: "Password: "
    then:
    - send-text: "paSSw0rd"

The 'watch' method works if the command output (e.g. subscription-manager) is a complete line but subscription-manager prompts for the password and does not send a newline after Password: so the watch will not run. If the command does not send a newline after the patter then we have to use the prompt option.

prompt and response

The prompt option matches output without waiting for a newline so it can respond to prompts from interactive commands.

- sh:
    command: subscription-manager register --username=me@domain.com
    prompt:
      "Password: " : "paSSw0rd"

When to prompt versus watch

use the prompt option if the command’s output will be on the same line as the cursor and you can match the end of the output. Use watch if you want to match regex to part of the output and it will appear in a complete line of text.