Skip to content

Commit 8d2c22c

Browse files
author
benoit-cty
committed
Process mode
1 parent fff1c79 commit 8d2c22c

2 files changed

Lines changed: 80 additions & 11 deletions

File tree

codecarbon/cli/main.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -347,36 +347,43 @@ def config():
347347
)
348348
def run(
349349
ctx: typer.Context,
350+
log_level: Annotated[
351+
str, typer.Option(help="Log level (critical, error, warning, info, debug)")
352+
] = "error",
350353
):
351354
"""
352355
Run a command and track its carbon emissions.
353356
354-
This command wraps any executable and measures the machine's total power
357+
This command wraps any executable and measures the process's total power
355358
consumption during its execution. When the command completes, a summary
356359
report is displayed and emissions data is saved to a CSV file.
357360
358-
Note: This tracks machine-level emissions (entire system), not just the
359-
specific command. For process-specific tracking, the command must be
360-
instrumented with the CodeCarbon Python library.
361+
Note: This tracks process-level emissions (only the specific command), not the
362+
entire machine. For machine-level tracking, use the `monitor` command.
361363
362364
Examples:
363365
364-
# Run any shell command
366+
Do not use quotes around the command. Use -- to separate CodeCarbon args.
367+
368+
# Run any shell command:
365369
codecarbon run -- ./benchmark.sh
366370
367-
# Commands with arguments (use single quotes for special chars)
371+
# Commands with arguments (use single quotes for special chars):
368372
codecarbon run -- python -c 'print("Hello World!")'
369373
370-
# Pipe the command output
374+
# Pipe the command output:
371375
codecarbon run -- npm run test > output.txt
372376
377+
# Display the CodeCarbon detailed logs:
378+
codecarbon run --log-level debug -- python --version
379+
373380
The emissions data is appended to emissions.csv (default) in the current
374381
directory. The file path is shown in the final report.
375382
"""
376383
# Suppress all CodeCarbon logs during execution
377384
from codecarbon.external.logger import set_logger_level
378385

379-
set_logger_level("critical")
386+
set_logger_level(log_level)
380387

381388
# Get the command from remaining args
382389
command = ctx.args
@@ -388,8 +395,10 @@ def run(
388395
)
389396
raise typer.Exit(1)
390397

391-
# Initialize tracker with minimal logging
392-
tracker = EmissionsTracker(log_level="error", save_to_logger=False)
398+
# Initialize tracker with specified logging level
399+
tracker = EmissionsTracker(
400+
log_level=log_level, save_to_logger=False, tracking_mode="process"
401+
)
393402

394403
print("🌱 CodeCarbon: Starting emissions tracking...")
395404
print(f" Command: {' '.join(command)}")

docs/edit/usage.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,67 @@ The command line could also works without internet by providing the country code
5858
5959
codecarbon monitor --offline --country-iso-code FRA
6060
61-
Implementing CodeCarbon in your code allows you to track the emissions of a specific block of code.
61+
62+
Running Any Command with CodeCarbon
63+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
65+
If you want to track emissions while running any command or program (not just Python scripts), you can use the ``codecarbon run`` command.
66+
This allows non-Python users to measure machine emissions during the execution of any command:
67+
68+
.. code-block:: console
69+
70+
codecarbon run -- <your_command>
71+
72+
Do not surround ``<your_command>`` with quotes. The double hyphen ``--`` indicates the end of CodeCarbon options and the beginning of the command to run.
73+
74+
**Examples:**
75+
76+
.. code-block:: console
77+
78+
# Run a shell script
79+
codecarbon run -- ./benchmark.sh
80+
81+
# Run a command with arguments (use quotes for special characters)
82+
codecarbon run -- bash -c 'echo "Processing..."; sleep 30; echo "Done!"'
83+
84+
# Run Python scripts
85+
codecarbon run -- python train_model.py
86+
87+
# Run Node.js applications
88+
codecarbon run -- node app.js
89+
90+
# Run tests with output redirection
91+
codecarbon run -- npm run test > output.txt
92+
93+
# Display the CodeCarbon detailed logs
94+
codecarbon run --log-level debug -- python --version
95+
96+
**Output:**
97+
98+
When the command completes, CodeCarbon displays a summary report and saves the emissions data to a CSV file:
99+
100+
.. code-block:: console
101+
102+
🌱 CodeCarbon: Starting emissions tracking...
103+
Command: bash -c echo "Processing..."; sleep 30; echo "Done!"
104+
105+
Processing...
106+
Done!
107+
108+
============================================================
109+
🌱 CodeCarbon Emissions Report
110+
============================================================
111+
Command: bash -c echo "Processing..."; sleep 30; echo "Done!"
112+
Emissions: 0.0317 g CO2eq
113+
Saved to: /home/user/emissions.csv
114+
⚠️ Note: Measured entire machine (includes all system processes)
115+
============================================================
116+
117+
.. note::
118+
The ``codecarbon run`` command tracks process-level emissions (only the specific command), not the
119+
entire machine. For machine-level tracking, use the ``codecarbon monitor`` command.
120+
121+
For more fine-grained tracking, implementing CodeCarbon in your code allows you to track the emissions of a specific block of code.
62122

63123
Explicit Object
64124
~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)