Skip to main content

attach

This command attaches to a running job making future log messages being written to stderr.

The command blocks until the job execution terminates, writing the job output message to stdout (if succeeded).

Ultimately it returns the original exit code of the job container to the caller.

info

If you are interested in getting past log messages use bx logs instead.

Usage

bx attach [--help] <jobId>

Parameters, inputs and exit codes

ShorthandLong versionDescription
--helpShow this help message and exit

Examples

Attach to a running job

Submit a job using the hello-world image.

$ bx submit tutorial/hello-world:1.0.0 '{"yourName":"Michael Jordan"}'
[batchx] [INFO] [2020/03/17 19:09:30] Submitting job...
[batchx] [INFO] [2020/03/17 19:09:30] Job submitted with id 1130

Use the jobs command to display all active jobs (in case you don't know the id of your job).

$ bx jobs
ID IMAGE AGO USER BX-UNITS INPUT STATUS COST/H RUNTIME COST
1130 b@t/hello-world:1.0.0 57 s david 1x1000x0 SUBMITTED 0.34 USD 0.00 USD

Then run bx attach with the id of the job. You will now start receiving live streams from this job.

$ bx attach 1130
[batchx] [INFO] [2020/03/17 19:10:43] Attaching to job 1130
[batchx] [INFO] [2020/03/17 19:10:44] Job status: SUBMITTED
[batchx] [INFO] [2020/03/17 19:12:46] Job status: STARTING
[batchx] [INFO] [2020/03/17 19:12:46] Job status: DOWNLOADING_INPUT
[batchx] [INFO] [2020/03/17 19:12:47] Job status: RUNNING
[batchx@tutorial/hello-world:1.0.0] [2020/03/17 19:12:53] Environment:
[batchx@tutorial/hello-world:1.0.0] [2020/03/17 19:12:53] environ({'HOSTNAME': 'batchx-container', 'PYTHON_PIP_VERSION': '19.1.1', 'SHLVL': '1', 'HOME': '/root', 'GPG_KEY': '0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D', 'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'BX_MEMORY': '1000', 'LANG': 'C.UTF-8', 'BX_GPUS': '0', 'PYTHON_VERSION': '3.6.8', 'BX_VCPUS': '1', 'PWD': '/batchx'})
[batchx] [INFO] [2020/03/17 19:13:09] Job status: UPLOADING_OUTPUT
[batchx] [INFO] [2020/03/17 19:13:10] Job status: SUCCEEDED
{"responseFile":"bx://jobs/1130/output/response/response.txt"}
tip

You can press ctrl-c at any point to detach from a running job without worrying on canceling or altering its state.

Attach to a finished job

This command is idempotent, so you can also attach to a finished job.

For this example we will first show the latest jobs with the jobs -a -l 2 command, which will display the last two jobs that were submitted (regardless of their status).

$ bx jobs -a -l 2
ID IMAGE AGO USER BX-UNITS INPUT STATUS COST/H RUNTIME COST
1131 b@t/hello-world:1.0.0 98 s david 1x1000x0 CANCELLED 0.34 USD 0 s 0.00 USD
1130 b@t/hello-world:1.0.0 4 m david 1x1000x0 SUCCEEDED 0.34 USD 23 s 0.01 USD

As shown in the STATUS column, the job 1130 has SUCCEEDED __ while the job 1131 was CANCELLED.

Attaching to the succeeded job 1130 shows information about its status at stderr and the output message at stdout, as well as returns a zero exit code.

$ bx attach 1130
[batchx] [INFO] [2020/03/17 19:28:03] Attaching to job 1130
[batchx] [INFO] [2020/03/17 19:28:03] Job status: SUCCEEDED
{"responseFile":"bx://jobs/1130/output/response/response.txt"}

$ echo $?
0

On the other side, attaching to the job 1131shows information about its status at stderr but no output message at stdout, as well as returns a non-zero exit code.

$ bx attach 1131
[batchx] [INFO] [2020/03/17 19:32:45] Attaching to job 1131
[batchx] [INFO] [2020/03/17 19:32:45] Job status: CANCELLED

$ echo $?
1
tip

This behavior makes this command pipe-able.