[Python O & M] Simple Python O & M script and python O & M script

Source: Internet
Author: User

[Python O & M] Simple Python O & M script and python O & M script
Background

Recently I used the Linux subsystem on Windows 10 and found that it has a very bad character: the Linux subsystem is not in the power-on and shutdown state, and is automatically loaded every time it enters the Bash shell, after exiting, all processes in the Linux subsystem will be shut down. If you try to run services such as Mysql at any time, you must enable the Bash shell at any time, even worse, these services are not automatically started as they enter the Bash shell, so I have to write a Python script to manage these services.

Related Technologies

Python3, argparse module

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

 

Start running service
(Env) root @ DESKTOP-1DDIIV2 :~ # Python pyops. py all-sinitctl: Unable to connect to Upstart: Failed to connect to socket/com/ubuntu/upstart: deny connection * 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: Unable to connect to Upstart: Failed to connect to socket/com/ubuntu/upstart: refuse connection * Stopping internet superserver xinetd [OK] (env) root @ DESKTOP-1DDIIV2 :~ #
Restart service
(Env) root @ DESKTOP-1DDIIV2 :~ # Python pyops. py xinetd-rxinetdinitctl: Unable to connect to Upstart: Failed to connect to socket/com/ubuntu/upstart: drop connection * Stopping internet superserver xinetd [OK] * Starting internet superserver xinetd [OK] (env) root @ DESKTOP-1DDIIV2 :~ #
Feelings

Python scripts are much easier to write than shell scripts.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.