[Python] How to check if a Cron job is running on MacOS

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a cron job running in Django every minute that gets a random stock ticker from the S&P 500 and updates the user interface of my app. When I run python manage.py runcrons, the job runs and I can verify by looking at the output in the terminal.

    But when my app is running, the ticker on the UI does not update every minute as it should. The UI code works fine, and it has been tested, so I am sure it is the backend. I tried to check the logs using grep CRON /var/log/syslog but it says that the file does not exist. I also tried to update the cron job so it logs to cron.log, but that does not seem to work either. I also checked python manage.py crontab show and I see the job there: Currently active jobs in crontab: crontab -> ('* * * * *', 'api.cron.DailyTickerCronJob').

    The weird part is that if I manually run the cron job the UI updates. How can I check that the cron job is running every minute? I want the user interface to update the ticker every minute. Thank you!

    settings.py:

    CRON_CLASSES = [
    "api.cron.DailyTickerCronJob",
    ]

    CRONJOBS = [
    ('* * * * *', "api.cron.DailyTickerCronJob"),
    ]


    cron.py:

    class DailyTickerCronJob(CronJobBase):
    schedule = Schedule(run_every_mins=1)
    code = 'api.daily_ticker_cron' # has to be unique

    def do(self):
    select_daily_ticker()
    print("yay!")


    utils.py

    def select_daily_ticker():
    today = date.today()
    DailyTicker.objects.all().delete()

    # pick and create a new Daily Ticker object
    symbol = random.choice(TICKERS)
    DailyTicker.objects.create(date=today, symbol=symbol)
    print(f"New ticker selected: {symbol} for {today}")


    crontab -e:

    * * * * * /Users/joe/anaconda3/bin/python /Users/joe/Desktop/App/backend/manage.py crontab run 5292ae840dbb57827b9a2fb5e9e88562

    Continue reading...

Compartilhe esta Página