linux timed task crontab

For specific crontab content, see

http://blog.csdn.net/zwhfyy/article/details/34065187

 ubuntu 重启crontab

sudo service cron restart 

 

Restart command not for ubuntu version

$ service crond restart

 

I have a requirement to automatically publish articles based on time, but the django environment uses virtualenv again

 

#!/usr/bin/env python
# encoding: utf-8

#--------------------setup django env start-----------------------------
import socket
import sys
import them
import django

PRODUCTION_HOST = [
    'ip-172-31-20-231',
    'ip-172-31-21-123',
]

DEV_HOST =[
    'w31',
]

PATH = '/home/david/xxx/xxx/xxx'
#PATH = '/ring/xxx/xxx/xxx'

sys.path.insert(0, PATH)
if socket.gethostname() in PRODUCTION_HOST:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings.production")
elif socket.gethostname() in DEV_HOST:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings.dev")
else:
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xxx.settings.local")
django.setup()

#--------------------setup django env end-----------------------------

import datetime
from article.models import Article

now = datetime.datetime.now()
qs = Article.objects.filter(is_home_featured=True).order_by('-date_modified')[:200]

for atricle in qs:
    print atricle.datetime_publish, atricle.datetime_unpublish
    if atricle.datetime_publish <= now <= atricle.datetime_unpublish:
        atricle.is_approved = True
    else:
        atricle.is_approved = False
    atricle.save()

  

use a sh to enter the virtualenv

#!/bin/bash

# local
cd /home/david/.virtualenvs/xxx
source ./bin/activate
python /home/david/xxx/xxx/xxx/scripts/schedule_publish_article.py
deactivate

# dev




# production

 

You may also need to change the permissions of these two files to executable.

chmod a+x xxxx.py

 

crontab -e

then add a sentence

*/50 * * * * bash /home/david/xxx/xxx/xxx/scripts/schedule_publish_article.sh

 

link a reference

http://www.centoscn.com/CentOS/help/2014/1030/4025.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326799518&siteId=291194637