> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/open-source/use-harness-open-source/pipelines-1/steps/background.md).

# Background

[Background steps](https://github.com/iKettles/harness-gitbook/tree/main/open-source/use-harness-open-source/reference/pipelines/yaml/step-background/README.md) run dependent services for the duration of a stage.

{% hint style="info" %}
Background step exit codes are ignored. A non-zero exit code does not fail the overall pipeline.

Background containers tend to exit with a non-zero exit code, since they often need to be killed after the pipeline completes.
{% endhint %}

## Communication <a href="#communication" id="communication"></a>

Background containers are reachable at a hostname identical to the container name.

This pipeline has a `ping` step that communicates with a Redis background step named `cache`.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: cache
        type: background
        spec:
          container: redis

      - name: ping
        type: run
        spec:
          container: redis
          script: |-
            redis-cli -h cache ping
```

It is important to remember that after a container is started, the software running inside the container (e.g. redis) takes time to initialize and begin accepting connections.

There are two approaches to this problem:

1. Add a [health check](#health-check) (preferred)
2. Add a [sleep](#sleep)

### Health check <a href="#health-check" id="health-check"></a>

Use a commandline tool to check if a service is up and running.

This pipeline runs MySQL as a background step, with a run step that uses the `mysqladmin` tool to check if the MySQL server is available. Once the database is ready, the database creation command runs.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: database
        type: background
        spec:
          image: mysql:8.0
          envs:
            MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
            MYSQL_DATABASE: gitness_db
            MYSQL_PASSWORD: gitness
            MYSQL_USER: gitness

      - name: healthcheck
        type: script
        spec:
          image: mysql:8.0
          run: |-
            while ! mysqladmin ping -h database -u gitness -pgitness --silent; do sleep 1; done
            mysql -h database -u gitness -pgitness -e "CREATE TABLE IF NOT EXISTS gitness_db.pipelines (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL);"
```

### Sleep <a href="#sleep" id="sleep"></a>

Give the background step adequate time to initialize before attempting to connect.

A simple solution is to use the `sleep` command.

```yaml
kind: pipeline
spec:
  stages:
  - type: ci
    spec:
      steps:
      - name: cache
        type: background
        spec:
          container: redis

      - name: ping
        type: run
        spec:
          container: redis
          script: |-
            sleep 5
            redis-cli -h cache ping
```
