简体   繁体   中英

Check Linux service status and start it if stopped using a Python script

I have a fixed list of services in a Linux server. I want to check the status of these services and start the service if it's stopped. I'm able to get this done using a shell script, but I'm looking for a Python script to do this.

You can use python subprocess.Popen or os.system .

Example:

subprocess.Popen(["sudo", "service", "redis", "start"])

subprocess.Popen(["sudo", "service", "redis", "status"], stdout=subprocess.PIPE)

You can read the stdout and check if the process is stopped and also see the exist status.

Alternatively you can use psutil to monitor processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM