def crontab(request): runmodel="/home/cheungssh/mysite/mysite/cheungssh/" callback=request.GET.get('callback') value=request.GET.get('value') runtime=request.GET.get('runtime') runtype=request.GET.get('type') info={"msgtype":"ERR","content":""} crond_status=commands.getstatusoutput('/etc/init.d/crond status') crondlog_value={} if not crond_status[0]==0: info['content']=crond_status[1] print 'crond没有启动' else: try: value=eval(value) if not type({})==type(value): info['content']="数据类型错误" else: fid=str(random.randint(90000000000000000000,99999999999999999999)) lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) value['fid']=fid value['user']=request.user.username value['status']='未启动'.decode('utf-8') value['runtime']=runtime value['cmd']="" value['lasttime']=lasttime value['runtype']=runtype value['createtime']=str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) value_to_log={} value_tmp=json.dumps(value) if runtype=="upload" or runtype=="download": value_to_log[value['fid']]=value runmodel_program=os.path.join(runmodel,"daemon_FileTransfer.py") cmd="""%s %s '%s' #%s""" % (runtime,runmodel_program,value_tmp,value['fid']) a=open(crond_file,'a') a.write(cmd+"\n") a.close() crond_write=commands.getstatusoutput("""/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0])==0: info['msgtype']='OK' crond_record.crond_record(value_to_log) else: delcmd=commands.getstatusoutput("""sed -i '/%s/d' %s""" % (fid,crond_file)) print delcmd,11111111111 info['content']=crond_write[1] print 'Runtime: ',runtime elif runtype=="cmd": hostinfo=request.GET.get('value') try: hostinfo=eval(hostinfo) value['cmd']=hostinfo['cmd'] value_to_log[value['fid']]=value cmdcontent= "\n%s#%s#%s\n" %(hostinfo['cmd'],hostinfo['id'],value['fid']) try: with open(cmdfile,'a') as f: f.write(cmdcontent) crondcmd=""" %s %s %s\n""" % (runtime,'/home/cheungssh/bin/cheungssh_web.py',fid) try: with open(crond_file,'a') as f: f.write(crondcmd) crond_write=commands.getstatusoutput("""/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0])==0: info['msgtype']='OK' crond_record.crond_record(value_to_log) else: print "加入计划任务失败",crond_write[1],crond_write[0] delcmd=commands.getstatusoutput("""sed -i '/%s/d' %s""" % (fid,crond_file)) info['content']=crond_write[1] except Exception,e: info['content']=str(e) except Exception,e: print '写入错误',e info['content']=str(e) except Exception,e: print '发生错误',e info['content']=str(e) else: info['content']="请求任务未知"
def crontab(request): runmodel = "/home/cheungssh/mysite/mysite/cheungssh/" callback = request.GET.get('callback') value = request.GET.get('value') runtime = request.GET.get('runtime') runtype = request.GET.get('type') info = {"msgtype": "ERR", "content": ""} crond_status = commands.getstatusoutput('/etc/init.d/crond status') crondlog_value = {} if not crond_status[0] == 0: info['content'] = crond_status[1] print 'crond没有启动' else: try: value = eval(value) if not type({}) == type(value): info['content'] = "数据类型错误" else: #value : fid,sfile,dfile,id,runtime,status,user fid = str( random.randint(90000000000000000000, 99999999999999999999)) lasttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) value['fid'] = fid value['user'] = request.user.username value['status'] = '未启动'.decode('utf-8') value['runtime'] = runtime value['cmd'] = "" value['lasttime'] = lasttime value['runtype'] = runtype value['createtime'] = str( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) value_to_log = {} #value_to_log[value['fid']]=value value_tmp = json.dumps(value) if runtype == "upload" or runtype == "download": value_to_log[value['fid']] = value runmodel_program = os.path.join(runmodel, "daemon_FileTransfer.py") cmd = """%s %s '%s' #%s""" % (runtime, runmodel_program, value_tmp, value['fid']) #该功能是前端访问两次, 生成了两次命令 a = open(crond_file, 'a') a.write(cmd + "\n") a.close() crond_write = commands.getstatusoutput( """/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0]) == 0: info['msgtype'] = 'OK' crond_record.crond_record(value_to_log) else: print "加入计划任务失败", crond_write[1], crond_write[0] info['content'] = crond_write[1] print 'Runtime: ', runtime elif runtype == "cmd": hostinfo = request.GET.get('value') try: hostinfo = eval(hostinfo) value['cmd'] = hostinfo['cmd'] value_to_log[value['fid']] = value #命令 服务器 fid cmdcontent = "\n%s#%s#%s\n" % ( hostinfo['cmd'], hostinfo['id'], value['fid'] ) ### cmd # selectserver # fid try: with open(cmdfile, 'a') as f: f.write(cmdcontent) #写入命令记录文件 #定义加载到crond文件中的格式: 时间 程序文件 fid crondcmd = """ %s %s %s\n""" % ( runtime, '/home/cheungssh/bin/cheungssh_web.py', fid) try: #写入crond文件 with open(crond_file, 'a') as f: f.write(crondcmd) #加载到计划任务中 crond_write = commands.getstatusoutput( """/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0]) == 0: info['msgtype'] = 'OK' crond_record.crond_record( value_to_log) #写入redis日志记录 else: print "加入计划任务失败", crond_write[ 1], crond_write[0] info['content'] = crond_write[1] ######### except Exception, e: info['content'] = str(e) except Exception, e: print '写入错误', e info['content'] = str(e) except Exception, e: print '发生错误', e info['content'] = str(e) else: info['content'] = "请求任务未知"
def crontab(request): runmodel="/home/cheungssh/mysite/mysite/cheungssh/" callback=request.GET.get('callback') value=request.GET.get('value') runtime=request.GET.get('runtime') runtype=request.GET.get('type') info={"msgtype":"ERR","content":""} if platform.dist()[0]=='Ubuntu': crond_status=(0,'') else: crond_status=commands.getstatusoutput('/etc/init.d/crond status') if not crond_status[0]==0: info['content']=crond_status[1] else: try: value=eval(value) if not type({})==type(value): info['content']="数据类型错误" else: fid=str(random.randint(90000000000000000000,99999999999999999999)) lasttime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()) value['fid']=fid value['user']=request.user.username value['status']='未启动'.decode('utf-8') value['runtime']=runtime value['cmd']="" value['lasttime']=lasttime value['runtype']=runtype value['createtime']=str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) value_to_log={} value_tmp=json.dumps(value) if runtype=="upload" or runtype=="download": value_to_log[value['fid']]=value runmodel_program=os.path.join(runmodel,"daemon_FileTransfer.py") cmd="""%s %s '%s' #%s""" % (runtime,runmodel_program,value_tmp,value['fid']) a=open(crond_file,'a') a.write(cmd+"\n") a.close() crond_write=commands.getstatusoutput("""/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0])==0: info['msgtype']='OK' crond_record.crond_record(value_to_log) else: delcmd=commands.getstatusoutput("""sed -i '/%s/d' %s""" % (fid,crond_file)) print delcmd,11111111111 info['content']=crond_write[1] print 'Runtime: ',runtime elif runtype=="cmd": hostinfo=request.GET.get('value') try: hostinfo=eval(hostinfo) value['cmd']=hostinfo['cmd'] value_to_log[value['fid']]=value cmdcontent= "\n%s#%s#%s\n" %(hostinfo['cmd'],hostinfo['id'],value['fid']) try: with open(cmdfile,'a') as f: f.write(cmdcontent) crondcmd=""" %s %s %s\n""" % (runtime,'/home/cheungssh/bin/cheungssh_web.py',fid) try: with open(crond_file,'a') as f: f.write(crondcmd) crond_write=commands.getstatusoutput("""/usr/bin/crontab %s""" % (crond_file)) if int(crond_write[0])==0: info['msgtype']='OK' crond_record.crond_record(value_to_log) else: print "加入计划任务失败",crond_write[1],crond_write[0] delcmd=commands.getstatusoutput("""sed -i '/%s/d' %s""" % (fid,crond_file)) info['content']=crond_write[1] except Exception,e: info['content']=str(e) except Exception,e: print '写入错误',e info['content']=str(e) except Exception,e: print '发生错误',e info['content']=str(e) else: info['content']="请求任务未知"