Python 定時執行指令碼

2021-05-08 10:56:51 字數 3953 閱讀 1489

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:[file2 file3...]

execute file(.py)     

-ft -filetime:[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 =

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()

linux定時執行指令碼

cron是乙個linux下的定時執行工具 cron是linux的內建服務,可以用以下的方法啟動 關閉這個服務 sbin service crond start 啟動服務 sbin service crond stop 關閉服務 sbin service crondrestart 重啟服務 sbin ...

Linux定時執行指令碼

在linux下的兩種定時執行任務的方法 at命令,以及crontab服務。1 at命令 假如我們只是想要讓特定任務執行一次,那麼,這時候就要用到at監控程式了。設定at命令很簡單,指示定執行的時間,那麼就會在哪個時候執行。at類似列印 程序,會把任務放到 var spool at目錄中,到指定時間執...

Loadrunner定時執行指令碼

coding utf 8 import time import os 格式為小時,分鐘,指令碼名稱 包括碟符,最好是放在根目錄下 需要把loadrunner安裝路徑的bin加入系統引數path下 run time 16,30,scenario1 17,30,scenario2 17,30,scena...