Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 943
CobraBytez Avatar asked May 16 '11 18:05

CobraBytez


3 Answers

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'
like image 184
Robᵩ Avatar answered Sep 19 '22 10:09

Robᵩ


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.

like image 36
Hyperboreus Avatar answered Sep 21 '22 10:09

Hyperboreus


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'
>>>
like image 45
Giampaolo Rodolà Avatar answered Sep 21 '22 10:09

Giampaolo Rodolà