Flask Run Crontab

This tutorial talk about how to execute a cronjob, from your flask project.

crontab -e

0 */01 * * * bash  /home/ubuntu/project/shellscript.sh

shellscript.sh

#!/bin/sh
source /home/ubuntu/project/venv/bin/activate
echo "Cron Job Running"
python /home/ubuntu/project/job.py
echo "Cron Job End"

Python code, job.py

from app import create_app, db
from app.product.models import *
from config import Config
from flask import request, current_app
import requests
import sys, os
import logging

class StandaloneConfig(Config):
    TESTING = True
    DEBUG = True
    
# application creation
app = create_app(StandaloneConfig)
app.app_context().push()

def do_something():
    current_app.logger.info("Run sample task");

do_something()


# if __name__ == '__main__':
#     main()
Flask Run Crontab

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.