sudo cat /var/spool/cron/root
sudo cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
crontab -l
sudo crontab -u root -l #查看指定用户的crontab
crontab -e
sudo crontab -u root -e #编辑指定用户的crontab
0 12 * * * (/usr/local/bin/python /data2/test2/data/GetDictData.py >/dev/null 2>&1 &)
#!/usr/bin/env python
#coding=utf-8
import urllib
import urllib2
import sys
import os
import time
import datetime
import socket
import copy
import logging
import logging.handlers
reload(sys)
sys.setdefaultencoding('utf8')
socket.setdefaulttimeout(2.0)
__version = '1.1'
__author = 'jimmylian'
etcdir = ''
datadir = ''
logdir = ''
def GetLogger(logdir, loglevel):
"""keep logfile change another file everyday"""
filename = r"/log_update"
myloglevel = 20
tmploglevel = int(loglevel) / 10 * 10
if tmploglevel >= 0 and tmploglevel <= 50:
myloglevel = tmploglevel
handler = logging.handlers.RotatingFileHandler(logdir+filename, maxBytes = 20*1024*1024, backupCount = 10)
fmt = '%(asctime)s [line:%(lineno)d] [%(levelname)s] %(message)s'
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
logger = logging.getLogger('GetDictData')
logger.addHandler(handler)
logger.setLevel(myloglevel)
return logger
def GetDateBefore(daydelta,split=''):
today = datetime.date.today()
bfyesterday = today - datetime.timedelta(days = daydelta)
if split != '':
fmt = '%Y'+split+'%m'+split+'%d'
else:
fmt = '%Y%m%d'
return bfyesterday.strftime(fmt)
def DownHttpFile(url, filename, logger):
tmpfile = "%s%s_tmp"%(datadir, filename)
try:
f = urllib2.urlopen(url)
data = f.read()
with open(tmpfile, 'wb') as code:
code.write(data)
except Exception, e:
logger.error("download file error %s"%e)
return False
filepath = "%s%s"%(datadir, filename)
os.rename(tmpfile, filepath)
return True
def process(logger):
url = "http://XXXX.cn/XXX_info_1_%s"%(GetDateBefore(1, ''))
flag = DownHttpFile(url, "info_1.txt", logger)
path = '%sinfo_1.txt'%(datadir)
destpath = '/info_1_%s.txt'%(GetDateBefore(1, ''))
if not flag:
if os.path.exists(destpath):
os.unlink(destpath)
logger.info("download file %s failed"%(url))
return
os.rename(path, destpath)
logger.info("download file %s done"%(url))
rmpath = '/info_1_%s.txt'%(GetDateBefore(3, ''))
if os.path.exists(rmpath):
os.remove(rmpath)
logger.info("remove file %s done"%(rmpath))
else:
logger.info("remove file %s failed"%(rmpath))
def process1(logger):
url = "http://XXXX.cn/XXX_info_2_%s"%(GetDateBefore(1, ''))
flag = DownHttpFile(url, "info_2.txt", logger)
path = '%sinfo_2.txt'%(datadir)
destpath = '/info_2_%s.txt'%(GetDateBefore(1, ''))
if not flag:
if os.path.exists(destpath):
os.unlink(destpath)
logger.info("download file %s failed"%(url))
return
os.rename(path, destpath)
logger.info("download file %s done"%(url))
rmpath = '/info_2_%s.txt'%(GetDateBefore(3, ''))
if os.path.exists(rmpath):
os.remove(rmpath)
logger.info("remove file %s done"%(rmpath))
#os.system("/data1/XXXXX/upload/apache-cat-8.5.71/bin/shutdown.sh")
#time.sleep(10)
#os.system("/data1/XXXXX/upload/apache-cat-8.5.71/bin/startup.sh")
#logger.info("restart tomcat success")
else:
logger.info("remove file %s failed"%(rmpath))
def __main__():
global datadir
global logdir
mybasedir = "%s"%(os.path.dirname(sys.argv[0]))
if mybasedir == '':
mybasedir = '.'
datadir = "%s/data/"%(mybasedir)
logdir = "%s/log/"%(mybasedir)
logger = GetLogger(logdir, 20)
runstr = ''
for keys in sys.argv:
runstr = "%s %s"%(runstr, keys)
logger.info(runstr)
process(logger)
process1(logger)
if __name__ == "__main__":
__main__()