Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all background pids in bash

Either I am not able to phrase my search correctly or the answer is not easy to find!, but I am trying to figure out how to list all of my background task PIDs. For example:

So far I have found that to list the last PID we use:

$!

But now I want to list the PID of the task before that (if one exists), but I can't find how to do that. Utlimatly I want to list all my background task PIDs.

I know we can also find last job ID with:

%% (last job in list)
%1 (first job in list)
%2 (second job in list)

But the same does not seem to work for process id?

Thanks all :)

like image 825
code_fodder Avatar asked Sep 18 '13 08:09

code_fodder


People also ask

How can I see all background processes?

You can use the ps command to list all background process in Linux. Other Linux commands to obtain what processes are running in the background on Linux. top command – Display your Linux server's resource usage and see the processes that are eating up most system resources such as memory, CPU, disk and more.

How do you find the PID of a background process in Unix?

The PID of the last executed command is in the $! For example, the $! expands to the process ID (PID) of the command/program most recently placed into the background, whether executed as an asynchronous command or using the bg command/builtin.

What bash command helps to find PID of last background process?

The PID of the last executed command is in the $! shell variable: my-app & echo $!


1 Answers

Use ps S. For example:

$ vim &
[1] 8263
$ ipython &
[2] 8264
$ ps S
 PID TTY      STAT   TIME COMMAND
 3082 pts/0    Ss     0:00 bash
 3137 pts/0    Sl+    0:00 python /usr/bin/ipython
 8207 pts/2    Ss     0:00 bash
 8263 pts/2    T      0:00 vim
 8264 pts/2    Tl     0:00 python /usr/bin/ipython
 8284 pts/2    Tl     0:00 python /usr/bin/ipython
 8355 pts/2    R+     0:00 ps S

If you want get PIDs use below:

$ ps S | awk '{print  $  1 }' | grep -E '[0-9]'
3082
3137
8207
8263
8264
8284
8357
8358
835

Also you can use jobs -l But it show background processes only for current session.

like image 78
Michael Kazarian Avatar answered Oct 18 '22 13:10

Michael Kazarian