Linux Command to Show Stopped and Running processes?

15,327

Solution 1

jobs -s list stopped process by SIGTSTP,no SIGSTOP. The main difference is that SIGSTOP cannot be ignored.

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 list PIDs who had received SIGSTOP? One way to get this is

 ps -e -o stat,command,pid | grep '^S '

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

Solution 2

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
Share:
15,327
Vimzy
Author by

Vimzy

Updated on June 16, 2022

Comments

  • Vimzy
    Vimzy almost 2 years

    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
    Vimzy over 8 years
    I have to use ps, and I need to make sure that both running and stopped processes are shown
  • Some programmer dude
    Some programmer dude over 8 years
    Job control and their commands are specific to the shell, there is no general jobs command, it's a shell built-in command.
  • vish4071
    vish4071 over 8 years
    @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
    vish4071 over 8 years
    @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)
  • Vimzy
    Vimzy over 8 years
    -e can't work because it processes that aren't a part of the group process ID
  • Some programmer dude
    Some programmer dude over 8 years
    Correct, there won't be such a command in the filesystem anywhere. Instead it's the shell that catches it and handles it.
  • vish4071
    vish4071 over 8 years
    @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.
  • ricardomenzer
    ricardomenzer almost 2 years
    In my case, jobs -l helped me to find the PID of a proccess stoped by ^Z.