From http://www.cnblogs.com/yixianclove/p/5824747.htmlBackground
Recently, using the Linux subsystem on Windows 10, it was found that it had a very pit-Daddy feature: The Linux subsystem is not in the state of the machine, each time into the bash shell is automatically loaded, after exiting the Linux subsystem all processes will be closed, If you run into a service like MySQL and you want to keep the bash shell open at any time, even if the service doesn't start automatically with the bash shell, I have to write a Python script to manage those services.
Related Technologies
Python3, Argparse module
Code
1From OSImportSystem2From ArgparseImportArgumentparser34DefStart_service (Service):5 System ("Service {} start". Format (Service))67DefStop_service (Service):8 System ("Service {} stop". Format (Service))910DefRestart_service (Service):11Print(service)System ("Service {} restart". Format (Service))1314DefManage_service ():Services =[]Services.append ("xinetd")Services.append ("Lighttpd")18ReturnServices1920DefSet_args ():Parser =Argumentparser ()Parser.add_argument ("Service", help ="The service to is managed.")Parser.add_argument ("-S","--start", help ="Start the service (s).", action ="Store_true")Parser.add_argument ("-R","--restart", help ="Restart the service (s).", action ="Store_true")Parser.add_argument ("-P","--stop", help ="Stop the service (s).", action ="Store_true")26ReturnParser.parse_args ()2728DefDeal (Args,services):29GlobalStart_service, Restart_service, Stop_serviceServices = ServicesIfNot Args.serviceELSE Servicesif 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 ())
Run Open 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:~#
Close Service
(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:~#
Restart Service
(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:~#
Feelings
Python scripts are much better than shell scripts.
<zz> "python ops" simple python Ops script