Repository: rclone/rclone (language: go)

Issue:
metrics: `rclone rc` binding metrics endpoint
<!--

We understand you are having a problem with rclone; we want to help you with that!

**STOP and READ**
**YOUR POST WILL BE REMOVED IF IT IS LOW QUALITY**:
Please show the effort you've put into solving the problem and please be specific.
People are volunteering their time to help! Low effort posts are not likely to get good answers!

If you think you might have found a bug, try to replicate it with the latest beta (or stable).
The update instructions are available at https://rclone.org/commands/rclone_selfupdate/

If you can still replicate it or just got a question then please use the rclone forum:

    https://forum.rclone.org/

for a quick response instead of filing an issue on this repo.

If nothing else helps, then please fill in the info below which helps us help you.

**DO NOT REDACT** any information except passwords/keys/personal info.

You should use 3 backticks to begin and end your paste to make it readable.

Make sure to include a log obtained with '-vv'.

You can also use '-vv --log-file bug.log' and a service such as https://pastebin.com or https://gist.github.com/

Thank you

The Rclone Developers

-->

#### The associated forum post URL from `https://forum.rclone.org`
I don't have any, just wanted to create an issue to link my future PR with.


#### What is the problem you are having with rclone?
`rclone rc` trying to bind metrics endpoint if environment variable `RCLONE_METRICS_ADDR` is set


#### What is your rclone version (output from `rclone version`)
v1.68.2


#### Which OS you are using and how many bits (e.g. Windows 7, 64 bit)
Linux 


#### Which cloud storage system are you using? (e.g. Google Drive)
Doesn't matter in that case.


#### The command you were trying to run (e.g. `rclone copy /tmp remote:tmp`)
`RCLONE_METRICS_ADDR="0.0.0.0:9000" rclone rc -vv rc/noop`


#### A log from the command with the `-vv` flag (e.g. output from `rclone -vv copy /tmp remote:tmp`)

NOTE: `└──╼ $ RCLONE_METRICS_ADDR="0.0.0.0:9000" rclone rcd` was run in advance.

```
└──╼ $ RCLONE_METRICS_ADDR="0.0.0.0:9000" rclone rc -vv rc/noop
2024/12/15 22:21:21 DEBUG : Setting  metrics_addr="0.0.0.0:9000" from environment variable RCLONE_METRICS_ADDR
2024/12/15 22:21:21 DEBUG : Setting --metrics-addr "0.0.0.0:9000" from environment variable RCLONE_METRICS_ADDR="0.0.0.0:9000"
2024/12/15 22:21:21 DEBUG : rclone: Version "v1.68.2" starting with parameters ["rclone" "rc" "-vv" "rc/noop"]
2024/12/15 22:21:21 CRITICAL: Failed to start metrics server: failed to init server: listen tcp 0.0.0.0:9000: bind: address already in use
```


<!--- Please keep the note below for others who read your bug report. -->

#### How to use GitHub

* Please use the 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to show that you are affected by the same issue.
* Please don't comment if you have no relevant information to add. It's just extra noise for everyone subscribed to this issue.
* Subscribe to receive notifications on status change and new comments.



Discussion / hints:

Fix patch:
diff --git a/cmd/cmd.go b/cmd/cmd.go
index 8f5f5f093e7c2..7e569c80be02f 100644
--- a/cmd/cmd.go
+++ b/cmd/cmd.go
@@ -429,11 +429,12 @@ func initConfig() {
 		fs.Fatalf(nil, "Failed to start remote control: %v", err)
 	}
 
-	// Start the metrics server if configured
-	_, err = rcserver.MetricsStart(ctx, &rc.Opt)
-	if err != nil {
-		fs.Fatalf(nil, "Failed to start metrics server: %v", err)
-
+	// Start the metrics server if configured and not running the "rc" command
+	if os.Args[1] != "rc" {
+		_, err = rcserver.MetricsStart(ctx, &rc.Opt)
+		if err != nil {
+			fs.Fatalf(nil, "Failed to start metrics server: %v", err)
+		}
 	}
 
 	// Setup CPU profiling if desired


Test patch: