- Heroku Cookbook
- Mike Coutermarsh
- 523字
- 2021-08-05 17:14:29
Running one-off tasks and dynos
In more traditional hosting environments, developers will often log in to servers to perform basic administrative tasks or debug an issue. With Heroku, we can do this by launching one-off dynos. These are dynos that contain our application code but do not serve web requests.
Note
For a Ruby on Rails application, one-off dynos are often used to run database migrations or launch a Rails console.
How to do it...
In this recipe, we will learn how to execute commands on our Heroku applications with the heroku run
command. Let's launch a terminal now to get started with the following steps:
- To have Heroku start a one-off dyno and execute any single command, we will use
heroku run
. Here, we can try it out by running a simple command to print some text to the screen:$ heroku run echo "hello heroku" Running `echo "hello heroku"` attached to terminal... up, run.7702 "hello heroku"
Note
One-off dynos are automatically shut down after the command has finished running.
- We can see that Heroku is running this command on a dyno with our application's code. Let's run
ls
to see a listing of the files on the dyno. They should look familiar:$ heroku run ls Running `ls` attached to terminal... up, run.5518 app bin config config.ru db Gemfile Gemfile.lock lib log Procfile public Rakefile README README.md tmp
- If we want to run multiple commands, we can start up a bash session. Type
exit
to close the session:$ heroku run bash Running `bash` attached to terminal... up, run.2331 ~ $ ls app bin config config.ru db Gemfile Gemfile.lock lib log Procfile public Rakefile README README.md tmp ~ $ echo "hello" hello ~ $ exit exit
- We can run tasks in the background using the
detached
mode. The output of the command goes to our logs rather than the screen:$ heroku run:detached echo "hello heroku" Running `echo hello heroku` detached... up, run.4534 Use `heroku logs -p run.4534` to view the output.
- If we need more power, we can adjust the size of the one-off dynos. This command will launch a bash session in a 2X dyno:
$ heroku run --size=2X bash
- If we are running one-off dynos in the detached mode, we can view their status and stop them in the same way we would stop any other dyno:
$ heroku ps === run: one-off processes run.5927 (1X): starting 2014/03/29 16:18:59 (~ 6s ago) $ heroku ps:stop run.5927
How it works…
When we issue the heroku run
command, Heroku spins up a new dyno with our latest slug and runs the command. Heroku does not start our application; the only command that runs is the command that we explicitly pass to it.
One-off dynos act a little differently than standard dynos. If we create one dyno in the detached mode, it will run until we stop it manually, or it will shut down automatically after 24 hours. It will not restart like a normal dyno will.
If we run bash from a one-off dyno, it will run until we close the connection or we reach an hour of inactivity.
- Getting Started with Citrix XenApp? 7.6
- WildFly:New Features
- Mobile Web Performance Optimization
- Python自動化運維快速入門
- Linux環境編程:從應用到內核
- Java應用開發技術實例教程
- 零基礎學單片機C語言程序設計
- C語言開發基礎教程(Dev-C++)(第2版)
- Kivy Cookbook
- 大學計算機應用基礎(Windows 7+Office 2010)(IC3)
- C#程序設計基礎入門教程
- AngularJS UI Development
- 零基礎C語言學習筆記
- Android熱門應用開發詳解
- Distributed Computing with Python