Use Hyperic to call Python to implement daemon
- 2020-06-23 00:52:01
- OfStack
Using Hyperic to call Python, the process daemon, for your reference, the specific content is as follows
Both Linux and Windows support calling operating system methods to get process information and determine whether a process exists. The difference is that the methods used to get process information and start the process are different.
The code is as follows:
#!/usr/bin/python
#-*- coding:utf-8 -*-
"""
Name: Process check script
The author: wjzhu
Time: 2014-06-30
Function: According to the name of the process, determine whether the process exists, perform the corresponding operation
Parameters: p_name : Process name |p_path : The process start path
The return value: 0: Process exists, exits normally |1 : The process does not exist. After executing the command, the process exists |2 : Other abnormal conditions
update:2014-07-29 Call before starting the process os.chdir() Method to change the current working directory and resolve the dependency of some processes on the working directory when they start
"""
import os
import sys
# Initializes the process name and command path
# Pass the program name and the program path as parameters
#p_name = sys.argv[1]
#p_path = sys.argv[2]
#Linux
p_name = "redis"
p_path = "/usr/local/redis-2.8.11/src/redis-server/usr/local/redis-2.8.11/redis.conf"
#Windows
#p_name = "filezilla.exe"
#p_path = "C:\\Program Files (x86)\\FileZilla FTP Client\\filezilla.exe"
#Linux Platform invoke ps The command /Win Platform invoke tasklist Command to determine whether a process exists, pass in the name of the process, and return the number of processes obtained for the query
def process_exit(process_name):
#Linux
p_checkresp = os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines()
#Windows To avoid process name truncation, the output format is csv , the use of tasklist /fo csv
#p_checkresp = os.popen('tasklist /fo csv | find "' + process_name + '"').readlines()
return len(p_checkresp)
#Linx Platform invoke os.system Method start command /Win Platform invoke os.startfile Method starts a command, passing in the command path, with no return value
def process_exec(process_path):
# Switch the working directory to the directory where the startup script resides, resolving the problem of relying on the working directory when some processes start
os.chdir(os.path.dirname(process_path))
#Linxu
os.system(process_path)
#Windows
#os.startfile(process_path)
# The main function
if __name__ == '__main__':
# Query process number is greater than 1 To return to 0 , do nothing, exit
if process_exit(p_name) >= 1:
print 0
sys.exit(0)
# Number of query processes is equal to 0
elif process_exit(p_name) == 0:
# Execute the start command
process_exec(p_path)
# Query process number is greater than 1 To return to 1 , launch successfully, exit
if process_exit(p_name) >= 1:
print 1
sys.exit(0)
# Startup failed, return 2 To quit
else:
print 2
sys.exit(0)
# Other questions, return 2 To quit
else:
print 2
sys.exit(0)