`sudo crontab -e
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/goro/.local/bin:/home/goro/bin
30 8 * * * cd /home/xxx/aaa; /usr/bin/python3 /home/goro/vget_plus/app/mycron.py
`
Djangoを実行するのであれば、カスタムコマンドを使用してみてください。
https://zenn.dev/wtkn25/articles/django-custom-command
カスタムコマンドで動くことを確認できたら、crontabで実行するように設定します。
毎日8時半にpythonファイルを実行したいだけであれば、下記で動くと思いますが、いかがでしょうか?
30 8 * * * cd /usr/bin/python3 /home/goro/vget_plus/app/mycron.py
動きません sudo crontab -e
00 05 01 * * sudo systemctl stop nginx; sudo letsencrypt renew; sudo systemctl start nginx
30 12 * * * cd /usr/bin/python3 /home/goro/vget_plus/app/mycron.py
mycron.py
from accounts.models import CustomUser
from django.core.mail import send_mail,send_mass_mail
import base64
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from email.mime.text import MIMEText
import time
from django.utils import timezone
from django.conf import settings
def cron_go(self,request, *args, **kwargs):
today = timezone.now().strftime('%Y-%m-%d')
vi_data = Vitalget.objects.filter(get_day=today)
user_count = vi_data.count()
user=[]
for vital in vi_data:
user.append(vital.user)
vi_item = CustomUser.objects.exclude(user_name__in=user)
msg = "Vgetmail send completely"
for vital in vi_item:
time.sleep(1)
name = vital.user_name
context={
'name':name,
'today':today
}
email = vital.email
name = vital.user_name
subject = "today VitalGet"
from_email = "360do.inc@gmail.com"
recipient_list = [email]
msg = render_to_string("mails/mail.txt",context)
send_mail(subject,msg,from_email, recipient_list)
return render(request,'app/vget_usermail.html')```