首页 > 代码库 > <zz>【Python运维】简单的Python运维脚本

<zz>【Python运维】简单的Python运维脚本

from http://www.cnblogs.com/yixianclove/p/5824747.html

背景

最近在Windows 10上使用Linux子系统,发现它有一个非常坑爹的特点:Linux子系统是没有开机关机状态的,每次进入Bash shell就自动载入,退出后Linux子系统的所有进程都会被关闭,如果你撞了Mysql之类的服务要想随时运行的话就要保持Bash shell的随时开启,更坑的是这些服务并不会随之进入Bash shell而自动启动, 我只好写一个Python脚本用于管理这些服务。

相关技术

Python3, argparse module

代码

 1 from os import system 2 from argparse import ArgumentParser 3  4 def start_service(service): 5     system("service {} start".format(service)) 6  7 def stop_service(service): 8     system("service {} stop".format(service)) 9 10 def restart_service(service):11     print(service)12     system("service {} restart".format(service))13 14 def manage_service():15     services = []16     services.append("xinetd")17     services.append("lighttpd")18     return services19 20 def set_args():21     parser = ArgumentParser()22     parser.add_argument("service", help = "the service to be managed.")23     parser.add_argument("-s", "--start", help = "start the service(s).", action = "store_true")24     parser.add_argument("-r", "--restart", help = "restart the service(s).", action = "store_true")25     parser.add_argument("-p", "--stop", help = "stop the service(s).", action = "store_true")26     return parser.parse_args()27 28 def deal(args,services):29     global start_service, restart_service, stop_service30     services = services if not args.service else services if args.service == "all"else [args.service]31     operation = start_service if args.start else restart_service if args.restart else stop_service32     for  service in services:33        operation(service)34 35 if __name__ == "__main__":36     deal(set_args(),manage_service())

 

运行

开启服务

(env) root@DESKTOP-1DDIIV2:~# python pyops.py all -sinitctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接 * Starting internet superserver xinetd                                                                                                                          [fail] * Starting web server lighttpd                                                                                                                                  [ OK ](env) root@DESKTOP-1DDIIV2:~#

关闭服务

(env) root@DESKTOP-1DDIIV2:~# python pyops.py xinetd -pinitctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接 * Stopping internet superserver xinetd                                                                                                                          [ OK ](env) root@DESKTOP-1DDIIV2:~#

重启服务

(env) root@DESKTOP-1DDIIV2:~# python pyops.py xinetd -rxinetdinitctl: 无法连接到 Upstart: Failed to connect to socket /com/ubuntu/upstart: 拒绝连接 * Stopping internet superserver xinetd                                                                                                                          [ OK ] * Starting internet superserver xinetd                                                                                                                          [ OK ](env) root@DESKTOP-1DDIIV2:~#

感想

Python脚本还是比shell脚本好写很多啊。

<zz>【Python运维】简单的Python运维脚本