pythonpopen会报错_Python subprocess.popen错误的pid

I'm trying to write a program that monitors gameservers in Python.

For that, n need to look up whether the process of the gameserver, which is started in a screen session is still running, and for that, i need it's pid, however, i don't get the pid of the screen session, i get a pid that only exists till the popen session is active. The pid of the screen session is one pid higher than the pid popen's .pid() method returns. I know that this could be caused through shell=True, but i'm running it with shell=False. Any clean way to get the pid of the screen process/gameserver in the screen process? My only other solution would be just to write .pid()+1, but i guess that would be a horrible workaround.

Here is the relevant part of my code:

proc = Popen(["screen", "-dmS", self.name, "./srcds_run", "-port", str(self.port), "-game", self.modDir], shell=False)

Thanks in advance.

解决方案

Your problem is not with python, it is with screen.

When you launch screen with -dmS and it goes into the background it does some forking and execveing by itself, therefore, a new PID, if you use strace to inspect the systemcalls, you will see how the screen binary gets executed in the returned PID that you can see, it later executes other processes and finishes.

It is the other process you are seeing in your ps calls

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值