How to execute a step in the background on CircleCI

If you want to monitor what is happening while running a job on CircleCI you can do this using a background step that will run alongside your other steps using background: true.

A step that has background: true set will run while there are other steps running but once they have finished the background step will also end, it will not run on its own if there are no other steps.

Instructions

The following code will run as an infinite loop printing the text from the echo command every five seconds as a background steps. Replacing the echo command will allow any command to run every 5 seconds. You can also set any step to run the background by adding the background: true to the end of the step as shown in the example below.

- run:
command: |
while true; do
sleep5
echo "this in running in the background"
done
background: true

Outcome

Any step which is set with background: true will continue to run in the background while other steps are running showing any output that you have defined.

Additional Resources

The following link is specifically for recording memory usage using the background: true setting:

https://support.circleci.com/hc/en-us/articles/360043994872-How-To-Record-a-Job-s-Memory-Usage

The following link is for the CircleCI documentation for background commands:

https://circleci.com/docs/configuration-reference/#background-commands

Was this article helpful?
0 out of 2 found this helpful

Comments

0 comments

Article is closed for comments.