Debian Codegraph @

README.md

raw symbols SCIP
1 # con-duct
2
3 [![codecov](https://codecov.io/gh/con/duct/graph/badge.svg?token=JrPazw0Vn4)](https://codecov.io/gh/con/duct)
4 [![PyPI version](https://badge.fury.io/py/con-duct.svg)](https://badge.fury.io/py/con-duct)
5 [![RRID](https://img.shields.io/badge/RRID-SCR__025436-blue)](https://identifiers.org/RRID:SCR_025436)
6
7 A lightweight wrapper that monitors the execution of commands and child processes, collecting stdout/stderr, resource usage data and system information.
8
9 The `con-duct` CLI provides multiple subcommands for working with execution data:
10 - **`run`**: Execute and monitor commands (also available via the `duct` convenience alias)
11 - **`pp`**: Pretty-print JSON logs
12 - **`plot`**: Visualize resource usage
13 - **`ls`**: List previous executions.
14
15 Also see our [Datalad Blog Post](https://blog.datalad.org/posts/intro-duct-tion/) for a hands on example, and
16 [duct-gallery](https://github.com/con/duct-gallery) for more samples with plots (contribute yours!).
17
18 It is inspired and based on [brainlife's smon](https://github.com/brainlife/abcd-spec/blob/master/hooks/smon) for usage stats collection and our [ReproMan](
19 https://github.com/ReproNim/reproman) for standard outputs capture.
20
21 ## Installation
22
23 Basic installation (includes `con-duct` and `duct` commands):
24
25 pip install con-duct
26
27 With optional helpers for visualization and analysis (`pp`, `plot`, `ls` commands):
28
29 pip install con-duct[all]
30
31 ## Quickstart
32
33 Try it out using either `duct` or `con-duct run`:
34
35 duct --sample-interval 0.5 --report-interval 1 test/data/test_script.py --duration 3 --memory-size=1000
36
37 `duct` is most useful when the report-interval is less than the duration of the script.
38
39 ## Documentation
40
41 - [Interpreting duct's resource statistics](docs/resource-statistics.md) — what `pcpu`, `rss`, and the `con-duct plot` chart actually measure, and where the numbers can mislead.
42
43 ## Reference
44
45 ### con-duct
46
47 <!-- BEGIN EXTRAS HELP -->
48 ```shell
49 >con-duct --help
50
51 usage: con-duct <command> [options]
52
53 A suite of commands to manage or manipulate con-duct logs.
54
55 positional arguments:
56 {run,pp,plot,ls} Available subcommands
57 run Execute a command with duct monitoring.
58 pp Pretty print a JSON log.
59 plot Plot resource usage for an execution.
60 ls Print execution information for all matching runs.
61
62 options:
63 -h, --help show this help message and exit
64 --version show program's version number and exit
65
66 ```
67 <!-- END EXTRAS HELP -->
68
69 ### con-duct run / duct
70
71 <!-- BEGIN HELP -->
72 ```shell
73 >duct --help
74
75 usage: con-duct run [-h] [-l {NONE,CRITICAL,ERROR,WARNING,INFO,DEBUG}] [-q]
76 [--version] [-p OUTPUT_PREFIX]
77 [--summary-format SUMMARY_FORMAT] [--colors] [--clobber]
78 [--sample-interval SAMPLE_INTERVAL]
79 [--report-interval REPORT_INTERVAL]
80 [--fail-time FAIL_TIME] [-c {all,none,stdout,stderr}]
81 [-o {all,none,stdout,stderr}]
82 [-t {all,system-summary,processes-samples}] [-m MESSAGE]
83 [--mode {new-session,current-session}]
84 command [command_args ...] ...
85
86 duct is a lightweight wrapper that collects execution data for an arbitrary
87 command. This command can be invoked as either 'duct' or 'con-duct run'.
88
89 Execution data includes execution time, system information, and resource usage
90 statistics of the command and all its child processes. It is intended to
91 simplify the problem of recording the resources necessary to execute a
92 command,
93 particularly in an HPC environment.
94
95 Resource usage is determined by polling (at a sample-interval).
96 During execution, duct produces a JSON lines (see https://jsonlines.org) file
97 with one data point recorded for each report (at a report-interval).
98
99 limitations:
100 Duct uses session id to track the command process and its children, so it
101 cannot handle the situation where a process creates a new session.
102 If a command spawns child processes, duct will collect data on them, but
103 duct exits as soon as the primary process exits.
104
105 environment variables:
106 Many duct options can be configured by environment variables (which are
107 overridden by command line options).
108
109 DUCT_LOG_LEVEL: see --log-level
110 DUCT_OUTPUT_PREFIX: see --output-prefix
111 DUCT_SUMMARY_FORMAT: see --summary-format
112 DUCT_SAMPLE_INTERVAL: see --sample-interval
113 DUCT_REPORT_INTERVAL: see --report-interval
114 DUCT_CAPTURE_OUTPUTS: see --capture-outputs
115 DUCT_MESSAGE: see --message
116 DUCT_CONFIG_PATHS: paths to .env files separated by platform path separator
117 (':' on Unix) (see below)
118
119 .env files:
120 Environment variables can also be set via .env files. By default, duct
121 searches the following locations (later files override earlier ones):
122
123 - /etc/duct/.env
124 - ${XDG_CONFIG_HOME:-~/.config}/duct/.env
125 - .duct/.env
126
127 Override the search paths by setting DUCT_CONFIG_PATHS with paths separated
128 by the platform path separator (':' on Unix)
129 (e.g., export DUCT_CONFIG_PATHS="/custom/path.env:~/.myduct.env").
130
131 Example .env file content:
132 # Set default log level
133 DUCT_LOG_LEVEL=DEBUG
134
135 # Configure intervals
136 DUCT_SAMPLE_INTERVAL=2.0
137 DUCT_REPORT_INTERVAL=120.0
138
139 # Set default output location
140 DUCT_OUTPUT_PREFIX=~/duct-logs/{datetime}-{pid}_
141
142 # Add execution notes (multiline)
143 DUCT_MESSAGE="Experiment run for paper revision
144 Using updated dataset from 2025-10-30
145 See lab notebook page 42 for details"
146
147 Supported .env syntax (via python-dotenv):
148 - KEY=value (basic assignment)
149 - KEY="value with spaces" (quoted values)
150 - KEY='single quotes' (single-quoted values)
151 - # comments (hash comments)
152 - Empty lines (ignored)
153 - Multiline values (use quotes)
154
155 Precedence (highest to lowest):
156 1. Command line arguments
157 2. Explicit environment variables
158 3. .env file values (later paths override earlier paths)
159 4. Hardcoded defaults
160
161 Notes:
162 - .env file support requires python-dotenv (pip install con-duct[all])
163 - DUCT_CONFIG_PATHS cannot be set in .env files (must be set before
164 loading)
165 - Malformed, unreadable, or missing .env files are skipped and logged
166
167 positional arguments:
168 command [command_args ...]
169 The command to execute, along with its arguments.
170 command_args Arguments for the command.
171
172 options:
173 -h, --help show this help message and exit
174 -l {NONE,CRITICAL,ERROR,WARNING,INFO,DEBUG}, --log-level {NONE,CRITICAL,ERROR,WARNING,INFO,DEBUG}
175 Level of log output to stderr, use NONE to entirely
176 disable. (default: INFO)
177 -q, --quiet [deprecated, use log level NONE] Disable duct logging
178 output (to stderr) (default: False)
179 --version show program's version number and exit
180 -p OUTPUT_PREFIX, --output-prefix OUTPUT_PREFIX
181 File string format to be used as a prefix for the
182 files -- the captured stdout and stderr and the
183 resource usage logs. The understood variables are
184 {datetime} and {pid}. Leading directories will be
185 created if they do not exist. You can also provide
186 value via DUCT_OUTPUT_PREFIX env variable. (default:
187 .duct/logs/{datetime}-{pid}_)
188 --summary-format SUMMARY_FORMAT
189 Output template to use when printing the summary
190 following execution. Accepts custom conversion flags:
191 !S: Converts filesizes to human readable units, green
192 if measured, red if None. !E: Colors exit code, green
193 if falsey, red if truthy, and red if None. !X: Colors
194 green if truthy, red if falsey. !N: Colors green if
195 not None, red if None (default: Summary: Exit Code:
196 {exit_code!E} Command: {command} Log files location:
197 {logs_prefix} Wall Clock Time: {wall_clock_time:.3f}
198 sec Memory Peak Usage (RSS): {peak_rss!S} Memory
199 Average Usage (RSS): {average_rss!S} Virtual Memory
200 Peak Usage (VSZ): {peak_vsz!S} Virtual Memory Average
201 Usage (VSZ): {average_vsz!S} Memory Peak Percentage:
202 {peak_pmem:.2f!N}% Memory Average Percentage:
203 {average_pmem:.2f!N}% CPU Peak Usage:
204 {peak_pcpu:.2f!N}% Average CPU Usage:
205 {average_pcpu:.2f!N}% )
206 --colors Use colors in duct output. (default: False)
207 --clobber Replace log files if they already exist. (default:
208 False)
209 --sample-interval SAMPLE_INTERVAL, --s-i SAMPLE_INTERVAL
210 Interval in seconds between status checks of the
211 running process. Sample interval must be less than or
212 equal to report interval, and it achieves the best
213 results when sample is significantly less than the
214 runtime of the process. Values below 1.0 behave
215 erratically. (default: 1.0)
216 --report-interval REPORT_INTERVAL, --r-i REPORT_INTERVAL
217 Interval in seconds at which to report aggregated
218 data. (default: 60.0)
219 --fail-time FAIL_TIME, --f-t FAIL_TIME
220 If command fails in less than this specified time
221 (seconds), duct would remove logs. Set to 0 if you
222 would like to keep logs for a failing command
223 regardless of its run time. Set to negative (e.g. -1)
224 if you would like to not keep logs for any failing
225 command. (default: 3.0)
226 -c {all,none,stdout,stderr}, --capture-outputs {all,none,stdout,stderr}
227 Record stdout, stderr, all, or none to log files. You
228 can also provide value via DUCT_CAPTURE_OUTPUTS env
229 variable. (default: all)
230 -o {all,none,stdout,stderr}, --outputs {all,none,stdout,stderr}
231 Print stdout, stderr, all, or none to stdout/stderr
232 respectively. (default: all)
233 -t {all,system-summary,processes-samples}, --record-types {all,system-summary,processes-samples}
234 Record system-summary, processes-samples, or all
235 (default: all)
236 -m MESSAGE, --message MESSAGE
237 Record a descriptive message about the purpose of this
238 execution. You can also provide value via DUCT_MESSAGE
239 env variable. (default: )
240 --mode {new-session,current-session}
241 Session mode: 'new-session' creates a new session for
242 the command (default), 'current-session' tracks the
243 current session instead of starting a new one. Useful
244 for tracking slurm jobs or other commands that should
245 run in the current session. (default: new-session)
246
247 ```
248 <!-- END HELP -->
249
250 ### Log Levels
251
252 Control duct's verbosity with `--log-level` (or `DUCT_LOG_LEVEL` environment variable):
253
254 | Level | Use when... |
255 |:-----:|:------------|
256 | `NONE` | Running in scripts where you only want the command's output |
257 | `ERROR` | You want silence unless something fails |
258 | `WARNING` | You want to see potential issues but not routine info |
259 | `INFO` | Normal interactive use (default) |
260 | `DEBUG` | Troubleshooting duct behavior or unexpected results |
261 | `CRITICAL` | You only want the most severe failures reported |
262
263 **INFO (default)** - Shows execution status and summary:
264 ```
265 $ duct echo "hello world"
266 2026-01-16T12:01:52 [INFO ] con-duct: duct is executing 'echo hello world'...
267 2026-01-16T12:01:52 [INFO ] con-duct: Log files will be written to .duct/logs/2026.01.16T12.01.52-12345_
268 hello world
269 2026-01-16T12:01:52 [INFO ] con-duct: Summary:
270 Exit Code: 0
271 Command: echo hello world
272 ...
273 ```
274
275 **NONE** - Silent operation, only shows command output:
276 ```
277 $ duct --log-level NONE echo "hello world"
278 hello world
279 ```
280
281 **DEBUG** - Verbose output for troubleshooting:
282 ```
283 $ duct --log-level DEBUG echo "hello world"
284 2026-01-16T12:02:02 [DEBUG ] con-duct: Searching for .env files in: /etc/duct/.env:...
285 2026-01-16T12:02:02 [DEBUG ] con-duct: .env file not found (skipping): .duct/.env
286 2026-01-16T12:02:02 [INFO ] con-duct: duct is executing 'echo hello world'...
287 2026-01-16T12:02:02 [DEBUG ] con-duct: Starting monitoring of the process...
288 ...
289 ```
290
291 ## FAQs
292
293 ### git-annex add keeps adding duct logs directly into git
294
295 By default, [git-annex](https://git-annex.branchable.com/) treats all dotfiles, and files under directories starting with a `.` as "small" regardless of `annex.largefiles` setting [[ref: an issue describing the logic](https://git-annex.branchable.com/bugs/add__58___inconsistently_treats_files_in_dotdirs_as_dotfiles/?updated#comment-efc1f2aa8f46e88a8be9837a56cfa6f7)].
296 It is necessary to set `annex.dotfiles` variable to `true` to make git-annex treat them as regular files and thus subject to `annex.largefiles` setting [[ref: git-annex config](https://git-annex.branchable.com/git-annex-config/)].
297 Could be done the repository (not just specific clone, but any instance since records in `git-annex` branch) wide using `git annex config --set annex.dotfiles true`.