36

I have simple python script which I would like to host on Heroku and run it every 10 minutes using Heroku scheduler. So can someone explain me what I should type on the rake command at the scheduler and how I should change the Procfile of Heroku?

2

2 Answers 2

44

Sure, you need to do a few things:

  1. Define a requirements.txt file in the root of your project that lists your dependencies. This is what Heroku will use to 'detect' you're using a Python app.

  2. In the Heroku scheduler addon, just define the command you need to run to launch your python script. It will likely be something like python myscript.py.

  3. Finally, you need to have some sort of web server that will listen on the proper Heroku PORT -- otherwise, Heroku will think your app isn't working and it will be in the 'crashed' state -- which isn't what you want. To satisfy this Heroku requirement, you can run a really simple Flask web server like this...

Code (server.py):

from os import environ
from flask import Flask

app = Flask(__name__)
app.run(environ.get('PORT'))

Then, in your Procfile, just say: web: python server.py.

And that should just about do it =)

Sign up to request clarification or add additional context in comments.

5 Comments

Heroku warns that the scheduler is not always super reliable. You may want to consider using Celery.
The web server is not necessary in this scenario and is wasting resources. You can simply scale the web server down to 0 instances, with the command: heroku ps:scale web=0
@rdegges what should requirements.txt in this case look like?
@torina the same as whatever your app's pip freeze output is. It's just a standard format that lists your project's dependencies.
For my project I had to add host with value 0.0.0.0 here app.run_server(host='0.0.0.0', port=environ.get('PORT')).
32

If you use free account [unverified*] on Heroku (so you cannot install addons), instead of using "Heroku scheduler", use time.sleep(n). You don't need Flask or any server in this case, just place script, say, inside folder Scripts (in default app/project by Heroku) and add to Procfile:
worker: python script.py.
Of course you replace script.py with Path to your script, including name,
ex. worker: python Scripts/my_script.py
Note: If your script uses third-party modules, say bs4 or requests, you need to install them in
pipenv install MODULE_NAME or create requirements.txt and place it where manage.py, Procfile, Pipfile, (etc) are. Next place in that requirements.txt:
requirements.txt:

MODULE_NAME==MODULE_VERSION

You can check them in pip freeze | grep MODULE_NAME
Finally deploy to Heroku server using git and run following command:

heroku ps:scale worker=1

That's it! Bot/Script is running, check it in logs:

heroku logs --tail

Source: https://github.com/michaelkrukov/heroku-python-script


unverified* - "To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site." It redirects to Credit Card info. However you can still have Free Acc, but you will not be able to use certain options for free users, such as installing addons:
https://devcenter.heroku.com/articles/getting-started-with-python#provision-add-ons

4 Comments

This answer is incorrect. You certainly CAN use the Heroku scheduler in a free app, as well as any other add-ons with a free tier.
@yonirabinovitch That's why you vote down? I am free tier, I cannot even touch addons at all, it says to provide credit card info. And I will not provide anything. So, Heroku demands you to "verify" account by providing Credit Card when attempting to use/install addons. Yes, my answer is correct. devcenter.heroku.com/articles/… ("To help with abuse prevention, provisioning an add-on requires account verification. If your account has not been verified, you will be directed to visit the verification site.") It redirects to Credit Card info.
Oh, OK. Fair enough. So edit your answer to indicate that it applies to unverified accounts, so that I can change my vote.
thanks for this! got my script working with minimal effort

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.