Skip to main content
 首页 » 编程设计

python之如何通过 Python 启动单一服务

2025年05月04日14linjiqin


我有一个这样的 bash shell:

server_home=`pwd` 
kill `cat $server_home'/AppService.pid'` 
if [ -f $server_home/AppService.pid ]; then 
    rm $server_home/AppService.pid 
fi 
export MONO_PATH=$server_home"/bin:"$server_home"/Function" 
mono-service2 -l:$server_home/AppService.pid $server_home/bin/AppService.exe 

它可以启动单声道服务。现在我需要做更多的工作,所以我写了一个像这样的 python shell:

import os 
server_home=os.getcwd() 
tmpPID = server_home +'/AppService.pid' 
tmpSS = server_home + '/bin/AppService.exe' 
os.popen("kill `cat '" + tmpPID +"'`").close() 
if os.path.isfile(tmpPID): 
    print "start delete pid" 
    os.remove(tmpPID) 
else: 
    print "cant find the " + tmpPID 
 
print  'mono-service2 -l:'+tmpPID+' '+tmpSS 
output = os.popen('mono-service2 -l:'+tmpPID+' '+tmpSS) 
print output.read() 
output.close() 

它将删除 AppService.PID 文件但不会启动该服务。我的代码有什么问题?请帮我。非常感谢。

请您参考如下方法:

os.popen的使用是deprecated .您是否尝试过使用 subprocess ?

subprocess.call(["mono-service2", "-l:" + tmpPID, tmpSS])