有时候需要脚本每隔一定的时间跑一下,每次写重复的程序是一种 浪费,为了良好的体现社会主义节约的精神,花 了点时间写了个小脚本,可以调用外部程序定时运行。

#ps.py

# coding=utf-8
import  sys
import  os
import  datetime
import  time

class  ArgsDealwith:
    
     def  arg_environment(self, args):
        filepath  =  ( ' PYTHON_PATH ' ,  ' path ' )
         for  i  in  filepath:
            filename  =  os.environ.get(i, " %s is null. "   %  i)
             print   ' %s: '   %  i, ' ' , filename

     def  arg_en(self, args):
        filepath  =  ( ' PYTHON_PATH ' ,  ' path ' )
         for  i  in  filepath:
            filename  =  os.environ.get(i, " %s is null. "   %  i)
             print   ' %s: '   %  i,  ' ' ,  ' ' .join(filename.split( ' ; ' ))

     def  arg_file(self, args):
         if   not  args:
             print   ' Error: file name is null. '
         else :
             if  len(args)  ==   1   and  args[0].lower()  ==   ' python_path ' :
                filename  =  os.environ.get(args[0], None)
                 if  filename:
                    args  =  filename.split( ' ; ' )
                 else :
                     print   ' Error: "%s" is null '   %  args[0]
                    exit()
                
             for  i  in  args:
                 if  os.path.isfile(i):
                     try :
                        execfile(i)
                     except :
                         print   ' Error: "%s" run failed. '   %  i
                 else :
                     print   ' Error: Not found "%s" file. '   %  i
            
     def  arg_filetime(self, args):
         if   not  args:
             print   ' Error: file name is null. '
         elif  len(args)  %   2   !=  0:
             print   ' Error: args isn't validate '
         else :
            fileTimes  =  [i  for  i  in  args  if  args.index(i)  %   2   !=  0 ]
             for  i  in  fileTimes:
                 try :
                    fileTimes[fileTimes.index(i)]  =  int(i)
                 except  Exception:
                     print   ' Error: time args isn't validate '
                    exit()
                 if  i  <=  0:
                     print   ' Error: time<=0 '
                    exit()                 
                
            minTime  =  fileTimes[0]
             for  i  in  fileTimes:
                 if  i  <  minTime:
                    minTime  =  i
            
            strTime  =  time.strftime( ' %Y-%m-%d-%H-%M-%S ' , time.localtime())
            tupleTime  =  tuple([int(i)  for  i  in  strTime.split( ' - ' )])
            begin  =  datetime.datetime(tupleTime[0], tupleTime[ 1 ], tupleTime[ 2 ], tupleTime[ 3 ], tupleTime[ 4 ], tupleTime[ 5 ])
            
            
            fileTimes  =  [[i, 0]  for  i  in  fileTimes]
            seconds  =  0
             while  True:
                strTime  =  time.strftime( ' %Y-%m-%d-%H-%M-%S ' , time.localtime())
                tupleTime  =  tuple([int(i)  for  i  in  strTime.split( ' - ' )])
                end  =  datetime.datetime(tupleTime[0], tupleTime[ 1 ],tupleTime[ 2 ], tupleTime[ 3 ], tupleTime[ 4 ], tupleTime[ 5 ])
                
                time_sub  =  end  -  begin
                seconds  =  time_sub.seconds
                begin  =  end
                 for  i  in  fileTimes:
                    i[ 1 ]  +=  seconds
                
                 for  i  in  fileTimes:
                     if  i[ 1 ]  ==  0:
                        self.arg_file( [args[args.index(str(i[0]))  -   1 ] ] )
                     elif  i[ 1 ]  >=  i[0]:
                        i[ 1 ]  %=  i[0]
                        self.arg_file( [args[args.index(str(i[0]))  -   1 ] ] )
                         if  i[0]  -  i[ 1 ]  <  minTime:
                            minTime  =  i[0]  -  i[ 1 ]
                
                time.sleep(minTime)

     def  arg_help(self, args):
        strHelp  =   " Usage: ps [-options] [args...] where option include: "
        strHelp  +=   """
        -? -help            print this help message
        -e -environment     print environment path
        -en                 print envrionment path per row
        -f -file:<file> [file2 file3...]
                            execute file(.py)      
        -ft -filetime:<file time> [file2 time2 file3 time3...]
                            execute file(.py) per time,
                            this run not stop,
                            but this command hasn't validate.  
                            time(seconds) must is interger and
                            not less than zero """
         print  strHelp
    
def  arg_args():
    args_dic  =  { ' arg_help '  : [ ' -? ' ,  ' -help ' ],  ' arg_environment '  : [ ' -e ' ,  ' -environment ' ],
                 ' arg_en '  : [ ' -en ' ],  ' arg_file '  : [ ' -f ' ,  ' -file ' ],  ' arg_filetime '  : [ ' -ft ' ,  ' filetime ' ]}
    argsCls  =  ArgsDealwith()
     if  len(sys.argv)  <=   1 :
        argsCls.arg_help(sys.argv)
     else :
        argsFun  =   ''
         for  i  in  sys.argv[ 1 :]:
            bMath  =  False
             for  j  in  args_dic.items():
                 if  i  in  j[ 1 ]:
                    argsFun  =  j[0]
                    bMath  =  True
                     break
             if  bMath:
                 break
         if  argsFun:
             try :
                getattr(argsCls, argsFun)(sys.argv[ 2 :])    
             except  Exception, error:
                 print  error
                exit()
         else :
             print   ' Error: "%s" isn't validate arg. '   %   '   ' .join(sys.argv[ 1 :])
             del  argsCls                    

if   __name__   ==   ' __main__ ' :
    arg_args()

Python:定时运行脚本相关推荐

  1. 新人初学python在运行脚本时报错了,不知道如何处理还请各位高人指点,拜托拜托

    新人初学python在运行脚本时报错了,不知道如何处理还请各位高人指点,拜托拜托 下面是报错: E + where False = <bound method LogIn.view_userna ...

  2. 树莓派学习笔记——crontab定时运行脚本

    crontab定时运行脚本 如果在玩树莓派的过程中,我们需要定时或者定期的运行一些程序或脚本,这里有个好方法,就是Linux下的 crontab. 通常会用到的就是两个命令: 查看已有的定时任务 pi ...

  3. python 多种运行脚本方式

    python 多种运行脚本方式 文章目录 python 多种运行脚本方式 1. 脚本与模块 2. 什么是 Python 解释器? 3. 如何以交互方式运行 Python 代码 4. 解释器如何运行 P ...

  4. 按键精灵--定时运行脚本的方法

    原文网址:按键精灵--定时运行脚本的方法_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍定时运行按键精灵的脚本的方法. 需求 早上的7点52分进行某个操作. 方案1:if判断+goto Dim ...

  5. python 定时运行 定时关闭_Python脚本用于定时关闭网易云音乐PC客户端

    本文主要讲述如何使用Python在指定的秒数后关闭Windows上运行的程序(此程序以网易云音乐为例).本文的背景是昨晚发现网易云音乐的PC客户端没有定时关闭的功能,可以使用Python编写一个简单的 ...

  6. CentOS 8.2云服务器定时运行脚本

    1.输入命令 crontab -e 2.输入文件执行的时间(输入格式如下) 命令提供到分钟运行脚本,5个*号依次分别代表分.时.日.月.周五类定时跑脚本的操作,具体操作样例如下: 1)每分钟跑一次脚本 ...

  7. python怎么运行脚本文件_python脚本怎么执行

    Python是一种计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发. pytho ...

  8. linux 定时运行脚本_教你如何在Linux下让Python脚本自启动和定时启动

    本文简单介绍如何在Linux下设置Python脚本自启动和定时启动. 一.Python开机自动运行 假如Python自启动脚本为auto.py.那么用root权限编辑以下文件: sudo vim /e ...

  9. python定时运行py文件_Python使用windows设置定时执行脚本

    如果你写了一些Python程序,想要在特定的时间进行执行,例如你想让一段爬虫程序在每天的上午10点执行一次,那么我们就可以来使用windows自带的定时任务进行设置.由于Windows系统,无法使用L ...

最新文章

  1. 神经网络的收敛标准有最优值吗?
  2. 高低压配电系统基础知识,都是非常全面的分享!
  3. 前端学习(2063):vue的生命周期
  4. nginx日志打印header_小水敲黑板--Nginx+Keepalived实现负载均衡和高可用
  5. c语言中用递归求平方根,下列给定程序中,函数fun()的功能是:应用递归算法求某数a的平方根。求平方根的迭代公式如下: 例如 - 赏学吧...
  6. 仿iPhone通讯录制作小程序自定义选择组件
  7. TIA博途软件中安装鼎实GSD文件的方法
  8. 台式机dp接口_常见视频接口图示及说明
  9. matlab的取整函数
  10. MDCC为移动开发者服务:一看、一聊、一聚
  11. 西门子博图功能指令——缩放
  12. js对节点 属性的操作
  13. 分享多张图片到微信朋友圈
  14. 计算机win7如何连接wifi网络,笔记本怎么连接wifi,教您笔记本怎么连接Wifi
  15. python单片机自动浇花_MicroPython实例之TPYBoardv102自动浇花系统
  16. 定义字符串的两种方式
  17. 原理+实战|7天带你学会GAN,生活从此乐无限
  18. sdl+ffmpeg视频播放器
  19. 爬虫(二):新浪财经爬虫(获取文本信息)
  20. 润乾报表的竞争对手到底是谁?

热门文章

  1. 宇宙最简单排序:桶排序
  2. elasticSearch学习笔记04-同义词,停用词,拼音,高亮,拼写纠错
  3. 如何启用计算机睡眠功能,如何让电脑休眠_如何开启电脑休眠模式-win7之家
  4. 90后,是被逼创业的,大家觉得呢?
  5. seo文章重复率高不利于收录
  6. Fadeln()方法
  7. java装逼的话_程序员装逼指南(语言篇)
  8. 当元气森林卖咖啡,能否再造一个爆款?
  9. 【基础篇】————21、隐匿攻击之Web Interface
  10. Win10下安装elasticsearch6.5.0