- 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.
- Cocos2d-x游戲開發(fā):手把手教你Lua語(yǔ)言的編程方法
- Python自動(dòng)化運(yùn)維快速入門
- QTP自動(dòng)化測(cè)試進(jìn)階
- Python數(shù)據(jù)結(jié)構(gòu)與算法(視頻教學(xué)版)
- C語(yǔ)言開發(fā)基礎(chǔ)教程(Dev-C++)(第2版)
- Learning AngularJS for .NET Developers
- 微課學(xué)人工智能Python編程
- Python自然語(yǔ)言理解:自然語(yǔ)言理解系統(tǒng)開發(fā)與應(yīng)用實(shí)戰(zhàn)
- 遠(yuǎn)方:兩位持續(xù)創(chuàng)業(yè)者的點(diǎn)滴思考
- Python硬件編程實(shí)戰(zhàn)
- INSTANT Apache Hive Essentials How-to
- WCF技術(shù)剖析(卷1)
- C語(yǔ)言程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)與習(xí)題精解
- Implementing Domain:Specific Languages with Xtext and Xtend
- Android從入門到精通