Forums

cron

Dear experts,

Can I run python file every minute from cron ?


20:31 ~/189/cron $ more /home/khsadigov/189/cron/run.sh

python /home/khsadigov/189/job.py

20:32 ~/189/cron $ crontab -l

1 * * * * /home/khsadigov/189/cron/run.sh

No cron is not enabled for users. We have scheduled tasks for hourly or daily tasks, but not every minute.

So I have a python script in Flask that is requesting JSON responses and passing those to an HTML template. Right now the responses are only updated when I reload my webapp or hourly as I have scheduled. Is there a better way to reload the JSON responses when someone views the HTML webpage?

I'm guessing that you've written your code so that you download some JSON stuff from an external website or something like that at the module level (that is, not inside a view function) and then your view renders the JSON using global variables created in that module-level code.

There are two ways to not have to reload the web app -- which one is best depends on how much time it takes to download the JSON data:

  • Download all of the JSON data in every hit to the view. This is the simplest solution, and will work if loading the JSON doesn't take much time.
  • Download the JSON data in a scheduled task, and then store it somewhere (in a file or in a database) then in your Flask view, load it up from the file or the database and return it.

With both of those solutions you would not need to reload the web app, except when you change its code.

Thanks Giles. Yeah, I'm pulling JSON stuff from Zendesk, very similar to this: https://support.zendesk.com/hc/en-us/articles/203691316-Zendesk-REST-API-tutorial-Python-edition. So I have 6 JSON urls I'm requesting from and rendering at the module level. The first option sounds better given it's simpler and I'm a bit of a newbie.

How do I download all the JSON data in every hit to the view?

Just take the code that you currently have outside the view function and put it inside.

That did it, thank you!

Great! :-)