> For the complete documentation index, see [llms.txt](https://docs.sonarsource.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sonarsource.com/sonarqube-cli/using-sonarqube-cli/exit-codes.md).

# Exit codes

A reference for the exit codes the SonarQube CLI returns, so you can integrate it into scripts, CI/CD jobs, and Git hooks.

The SonarQube CLI uses exit codes to signal what happened. Use them in scripts and your CI/CD environment to gate pipelines, fail builds, or run conditional checks.

| Code  | Meaning                                                                                                                                                                                                                                                                                                                                                            |
| ----- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `0`   | Success. The command completed and (where applicable) found nothing to report. `sonar remediate` also exits `0` when the [Remediation Agent](/agent-centric-development-cycle/in-your-long-living-branches-the-code-maintenance-loop/remediation-agent.md) is not eligible or not enabled for your organization (the CLI prints an informational message instead). |
| `1`   | Command failure. Network error, authentication failure, server error, or any other unhandled problem.                                                                                                                                                                                                                                                              |
| `2`   | Invalid options. You passed conflicting or unknown flags, or you omitted a required argument.                                                                                                                                                                                                                                                                      |
| `51`  | Findings or quality gate failure. `sonar analyze secrets` reports secrets, `sonar analyze` / `sonar analyze agentic` reports issues on the analyzed change set, or `sonar quality-gate status` reports a failed quality gate.                                                                                                                                      |
| `130` | Interrupted. You pressed `Ctrl+C` while the CLI was running.                                                                                                                                                                                                                                                                                                       |

> **Note:** Exit code 3 is not listed in this page's exit codes table. If you're seeing it, you're likely running `sonar-scanner` (SonarScanner CLI) rather than `sonar` (SonarQube CLI). These are separate products. See the SonarScanner CLI docs for [SonarQube Server](https://docs.sonarsource.com/sonarqube-server/analyzing-source-code/scanners/sonarscanner/) or [SonarQube Cloud](/sonarqube-cloud/analyzing-source-code/scanners/sonarscanner-cli.md).

## How to use these in scripts

### Treat findings as a failure

Because secrets and analysis findings exit with `51`, fail a CI step automatically:

```bash
sonar analyze secrets src/
# Step fails if secrets are present (exit 51), passes otherwise.
```

### Distinguish "no findings" from "command broke"

If you want to keep going on findings but stop on real errors, branch on the exit code:

```bash
sonar analyze --staged
case $? in
  0)   echo "Clean, no new issues." ;;
  51)  echo "Issues found, but analysis succeeded." ;;
  *)   echo "Analysis failed (exit $?)."; exit 1 ;;
esac
```

### Gate a CI step on quality gate status

`sonar quality-gate status` exits `51` when the quality gate fails, so a pipeline step fails automatically. When you omit `--branch` and `--pull-request`, the command auto-detects a pull request for the current git branch when SonarQube has analyzed exactly one matching pull request; otherwise it reports the project's default branch. Pass `--branch` or `--pull-request` when the pipeline must check a specific scope. For a file or directory path, exit `51` means that path has failing conditions; exit `0` also covers a `[· Not applicable]` result (no conditions apply to the path). A path that cannot be resolved in the project tree exits `2`.

```bash
sonar quality-gate status -p my-project
# Step fails when the quality gate does not pass (exit 51), passes otherwise.
```

### Tolerate `Ctrl+C` cleanly

In long-running pipelines, treat `130` as a user-initiated cancel and avoid retrying:

```bash
sonar analyze
rc=$?
if [ "$rc" -eq 130 ]; then
  echo "Cancelled."; exit 130
fi
```

## Related pages <a href="#related-pages" id="related-pages"></a>

* [Commands reference](/sonarqube-cli/using-sonarqube-cli/commands.md)
* [Environment variables](/sonarqube-cli/using-sonarqube-cli/environment-variables.md)
* [Output formats](/sonarqube-cli/using-sonarqube-cli/output-formats.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sonarsource.com/sonarqube-cli/using-sonarqube-cli/exit-codes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
