0

I have a django script to send a slack report about the number of object Open Position be created everyday and it should be run daily at 18:00. When I run the cron in my computer, it only work when I add SHELL=/bin/bash into the cron file).

SHELL=/bin/bash
* * * * * source /the_virtual_env_path/bin/activate && cd /my_project_path/ && ./manage.py daily_slack

But when I add the those commands to the cron on my Amazon EC2 instance, nothing happens. I tried to add some environment variables in the shell which not in cron environment (via this answer), restart the cron but it didn't work. But if I run the commands manually it run properly.

This is the code for my script:

# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand, CommandError
from slackclient import SlackClient
from human_resource.models.recruitment import OpenPosition, TimeMarkForSlackReport
import datetime
import os

slack_token = 'my_slack_token'
sc = SlackClient(slack_token)
contents = []
attachments = []
class Command(BaseCommand):
    def handle(self, *args, **options):
        today = datetime.date.today()
        today_open_position_queryset = OpenPosition.objects.filter(created_at__date=today)
        count = 0

        if(len(today_open_position_queryset) != 0):
            for open_position in today_open_position_queryset:
                count += 1
                open_position_details = [
                    {
                        "title": open_position.name,
                        "value": "Position: " + open_position.position.position_name
                        + " | Team: " + open_position.team.team_name
                        + " | Created at: " + open_position.created_at.time().strftime('%H:%M')
                        + " | Created by: " + str(open_position.created_by) +
                        " | <" +
                        "my_link" + str(open_position.pk) + "|Click here> for the open position link!",
                    },
                ]
                contents.extend(open_position_details)
            attachments = [
                        {
                            "color": "000000",
                            "fields": contents
                        }
                    ]
            sc.api_call(
                "chat.postMessage",
                link_names=1,
                channel='#my_channel',  # replace with channel or person
                username="Daily Report",
                text="Today Open Positions",
                attachments = attachments
            )

Does anyone have any advice or possible solutions? I've been struggling with this problem for days. Thanks for your time!

SOLVED: It seems like the time zone difference delays the execution of my commands

Cypherius
  • 521
  • 2
  • 14
  • Some ideas: https://stackoverflow.com/questions/3287038/cron-and-virtualenv – jarmod Aug 16 '17 at 11:08
  • (Not sure if it helps:) We are running Django on Elastic Beanstalk and orchestrate Cron via `django-kronos` package. It runs the management commands with `source /opt/python/current/env` prefix and it works fine. – petr Aug 16 '17 at 11:13
  • Well, it seems like the time zone difference caused it. Thanks for your solutions though! – Cypherius Aug 17 '17 at 06:54

0 Answers0