zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python服务器运维步骤_python运维服务器

2023-06-13 09:12:31 时间

大家好,又见面了,我是你们的朋友全栈君。

好久没有写东西了.一直做服务器开发需要写一些脚本来控制服务器的启动.本来Windows自带了任务计划,但不是特别方便,还是用python写了一下.

需求:在固定的时间启动服务器

先看源代码:

def start_process(date):

cwd = os.getcwd();

global list_threads;

global iskill;

iskill = False;

for i in range(num_threads):

t=Thread(target=start_service,args=(cwd, date, process_name[i]));

t.setDaemon(True);

t.start();

list_threads.append(t);

time.sleep(0.1)

time.sleep(1);

def start_service(cwd, date, name):

for (k,v) in process_condition.items():

if (k == name):

process_info = cwd+”\\”+k+” “+cwd+”\\”+v;

try:

p = subprocess.Popen(process_info, stdout=subprocess.PIPE);

print “%s starts successful\n” % name;

c = p.stdout.readline();

while 1:

#print c

c=p.stdout.readline();

p.stdout.flush();

time.sleep(0.01)

global iskill;

if (iskill):

print “now kill thread %s\n” % name;

quit();

break;

except Exception, p:

a = 0;

sys.exit();

注意点有几个地方:

服务器需要输出日志,所以不能在一个线程,否则日志在python的缓冲区,太多后会无法写出.

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/157891.html原文链接:https://javaforall.cn