Running Commands On Heroku

How to run commands on Heroku locally and from their web interface

Sometimes you will need to perform maintenance on your Heroku application. In this post, I will take you through the two main ways I run ad-hoc commands on Heroku applications.

Running Commands with Heroku CLI

When I am on my machine, I use the Heroku command line interface.

All commands for Heroku CLI start with heroku. To tell Heroku to run a command, you use run.

The full line you will need to run will look like

heroku run command_to_run arguments for command --app my_heroku_app
  • heroku run tells Heroku you want to run a command
  • command_to_run arguments for the command is where you would put your full command, like you were running it locally
  • --app my_heroku_app tells Heroku which app you want to run the command on

If I had a Ruby on Rails application and I want to run the db:seed command, I would write:

heroku run rails db:seed --app my_rails_app

When you run a command you will see it:

  • Connecting to the appropriate instance
  • Executing the command
  • Returning any output

Running Commands on the Heroku Web Interface

If I am not on my machine, then I will use the web interface. The web interface is excellent, you can share guidance with your team without assuming anyone has set up the CLI tool.

To use the web console, navigate to your application in Heroku. The same application that you would pass into --app earlier.

In the top-right of the interface, there is a “More” button, pressing it displays a “Run Console” option. Select it to add your command.

In this case, we don’t need to type heroku run or --app my_heroku_app because Heroku knows both these things.

You can even bookmark the console by editing this link with your details: https://dashboard.heroku.com/apps/MY-APP-NAME?web-console=MY-APP-NAME.


Recent posts View all

Ruby

How to ignore Bullet in RSpec tests

Using Bullet during a test can pick up mistakes but also has false negatives; here is an easy way to ignore them

Ruby

How we used Nokogiri to fix a sizing issue in YouTube's oEmbed

At some point, one of our calls to YouTube's oEmbed endpoint was returning videos way too small; we fixed in in our Rails application by using Nokogiri