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

孤人 提交于 2019-12-05 07:09:08

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'

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.

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