Is there a way to determine if a Linux PID is paused or not?

北城余情 提交于 2019-12-07 01:04:43

问题


I have a python script that is using the SIGSTOP and .SIGCONT commands with os.kill to pause or resume a process. Is there a way to determine whether the related PID is in the paused or resumed state?


回答1:


You can find information about a process from its /proc directory (/proc/<PID>). Specifically, you can find its run state with this python expression:

open(os.path.join('/proc', str(pid), 'stat')).readline().split()[2]=='T'

EDIT: This next expression fixes a (presumably rare) bug with the original:

re.sub(r'\(.*\)', '()', open(os.path.join('/proc', str(pid), 'stat')).readline()).split()[2]=='T'



回答2:


call ps and check the STAT value. D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S Interruptible sleep (waiting for an event to complete) T Stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z Defunct ("zombie") process, terminated but not reaped by its parent.




回答3:


You can use psutil:

>>> import psutil
>>> pid = 1243
>>> p = psutil.Process(pid)
>>> p.status
0
>>> str(p.status)
'running'
>>> p.status == psutil.STATUS_RUNNING
True
>>>
>>> p.suspend()
>>> p.status
3
>>> str(p.status)
'stopped'
>>> p.status == psutil.STATUS_STOPPED
True
>>>
>>> p.resume()
>>> str(p.status)
'running'
>>>


来源:https://stackoverflow.com/questions/6021771/is-there-a-way-to-determine-if-a-linux-pid-is-paused-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!