Vimzy
Vimzy

Reputation: 1965

Linux Command to Show Stopped and Running processes?

I'm presently executing the following Linux command in one of my c programs to display processes that are running. Is there anyway I can modify it to show stopped processes and running ones?

char *const parmList[] = {"ps","-o","pid,ppid,time","-g","-r",groupProcessID,NULL};
execvp("/bin/ps", parmList);

Upvotes: 10

Views: 35373

Answers (3)

Pablo Bianchi
Pablo Bianchi

Reputation: 2038

jobs -s list stopped process by SIGTSTP (20), no SIGSTOP (19). The main difference is that SIGSTOP cannot be ignored. More info with help jobs.

You can SIGTSTP a process with ^Z or from other shell with kill -TSTP PROC_PID (or with pkill, see below), and then list them with jobs.

But what about listing PIDs who had received SIGSTOP? One way to get this is

 ps -A -o stat,command,pid | grep '^T '

From man ps:

-A Select all processes. Identical to -e.

T stopped by job control signal


I found very useful this two to stop/cont for a while some process (usually the browser):

kill -STOP $(pgrep procName)
kill -CONT $(pgrep procName)

Or with pkill or killall:

pkill -STOP procName
pkill -CONT procName

Upvotes: 16

Wayne Walker
Wayne Walker

Reputation: 2466

Credit to @pablo-bianchi, he gave me the oompff (starting point) to find SIGSTOP'd and SIGTSTP'd processes, however his answers are not completely correct.

Pablo's command should use T rather than S

$ ps -e -o stat,command,pid | grep '^T '
T    /bin/rm -r 2021-07-23_22-00 1277441
T    pyt 999                     1290977
$ ps -e -o stat,command,pid | grep '^S ' | wc -l
153
$

From man ps:

PROCESS STATE CODES
       Here are the different values that the s, stat and state output specifiers (header "STAT"
       or "S") will display to describe the state of a process:

               D    uninterruptible sleep (usually IO)
               I    Idle kernel thread
               R    running or runnable (on run queue)
               S    interruptible sleep (waiting for an event to complete)
               T    stopped by job control signal
               t    stopped by debugger during the tracing
               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

WRT pgrep, it is a real grep, the argument is NOT a program name; rather, it is a regular expression applied to the first item in /proc//cmdline (usually the name from the executing commandline (or execve()).

Therefore if you are trying to kill pyt, you would accidentally also kill all the python programs that are running:

$ pgrep -a pyt
7228 python3 /home/wwalker/bin/i3-alt-tab-ww --debug
1290977 pyt 999

You need to "anchor" the regular expression:

$ pgrep -a '^pyt$'
1290977 pyt 999

Upvotes: 1

vish4071
vish4071

Reputation: 5277

ps -e lists all processes.

jobs list all processes currently stopped or in background.

So, you can run jobs command using execvp:

char *arg = {"jobs", NULL};
execvp(arg[0], arg);

Upvotes: 0

Related Questions