8

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);
Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • possible duplicate of [Make a system call to get list of processes](http://stackoverflow.com/questions/18408766/make-a-system-call-to-get-list-of-processes) – Pynchia Sep 30 '15 at 06:57

3 Answers3

14

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
Pablo Bianchi
  • 1,824
  • 1
  • 26
  • 30
1

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
Wayne Walker
  • 2,316
  • 3
  • 23
  • 25
0

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);
vish4071
  • 5,135
  • 4
  • 35
  • 65
  • I have to use ps, and I need to make sure that both running and stopped processes are shown – Vimzy Sep 30 '15 at 07:04
  • Job control and their commands are specific to the *shell*, there is no general `jobs` command, it's a shell built-in command. – Some programmer dude Sep 30 '15 at 07:06
  • @Vimzy, if you only need that both running and stopped processes are shown (and you don't need to differentiate), you can use `ps -e`. – vish4071 Sep 30 '15 at 07:15
  • @JoachimPileborg, does that mean `jobs` can't be used using `execvp`. I have not tried running this snippet (because I was sure it would work, execvp is used like this) – vish4071 Sep 30 '15 at 07:17
  • -e can't work because it processes that aren't a part of the group process ID – Vimzy Sep 30 '15 at 07:21
  • Correct, there won't be such a command in the filesystem anywhere. Instead it's the shell that catches it and handles it. – Some programmer dude Sep 30 '15 at 07:22
  • @JoachimPileborg, thanks for the insight. @vimzy, I don't really understand what you mean by `it processes that aren't a part of the group process ID`, because man page says it selects all processes. I don't really know but maybe `ps -A` or `ps aux` can be used. – vish4071 Sep 30 '15 at 07:32