Hidden Features of the Heroku CLI
Heroku’s CLI interface, the Heroku Toolbelt, has a lot of features which aren’t obvious, or are hidden away. Here are some of the useful ones I’ve found.
heroku run
Most people who use Heroku for Rails know about heroku run
. For Rails apps,
it’s used to run rake
all the time. But did you know you can run bash
and
explore your app?
$ heroku run bash
Running `bash` attached to terminal... up, run.1563
$ ls -1
bin
build
config
config.rb
....etc....
$ rm Gemfile # you can even delete files
I’ve used heroku run bash
to debug filesystem problems and detect files that
should be on Heroku, but were somehow missing in the compiled slug.
heroku open
heroku open
is simple: it will open your Heroku site in your web browser. If
you have a staging
Heroku remote, you could do heroku open --remote staging
.
Or you can use the --app
option and do heroku open --app sushi
.
heroku ps
Are your processes running smoothly after that last deploy? Check with heroku
ps
:
$ watch heroku ps
Every 2.0s: heroku ps -r staging Tue Dec 24 10:46:07 2013
=== web (1X): `bundle exec unicorn -p $PORT -c ./config/unicorn.rb`
web.1: starting 2013/12/20 17:21:48
I’ll let that run for a minute to ensure nothing crashes after a deploy.
heroku git:remote
heroku git:remote
can add a git remote to your app:
# Add a git remote named foobar that points to the app-staging application
heroku git:remote --app app-staging --remote foobar
This command would be a good addition to a bin/setup
script.
heroku addons
I’m sure you’ve used heroku addons:add newrelic
to add an addon, but how about
these two?
heroku addons:open newrelic
will open the New Relic dashboard in your browserheroku addons:docs newrelic
will open up Heroku’s documentation page for New Relic.
heroku pg:psql
Sometimes I need to run SQL against a Heroku database. I could do heroku
pg:psql
to get a psql prompt and type in my SQL, but what if I need to
run the same command a few times in a row? Then I use shell here strings, like
this:
heroku run pg:psql <<<"SELECT COUNT(*) FROM users WHERE email = 'gabebw@gabebw.com'"
This prints out the results immediately, and I can re-run it easily since the whole command is in my shell history.
Any suggestions?
Any suggestions? I’m available on Twitter: @gabebw.