The pstree
is a very good solution, but it is a little bit reticent. I use ps --forest
instead. But not for a PID
(-p
) because it prints only the specific process, but for the session (-g
). It can print out any information ps
can print in a fancy ASCII art tree defining the -o
option.
So my suggestion for this problem:
ps --forest -o pid,tty,stat,time,cmd -g 2795
If the process is not a session leader, then a little bit more trick has to be applied:
ps --forest -o pid,tty,stat,time,cmd -g $(ps -o sid= -p 2795)
This gets the session id (SID) of the current process first and then call ps again with that sid.
If the column headers are not needed add a '=' after each column definition in '-o' options, like:
ps --forest -o pid=,tty=,stat=,time=,cmd= -g $(ps -o sid= -p 2795)
An example run and the result:
$ ps --forest -o pid=,tty=,stat=,time=,cmd= -g $(ps -o sid= -p 30085)
27950 pts/36 Ss 00:00:00 -bash
30085 pts/36 S+ 00:00:00 \_ /bin/bash ./loop.sh
31888 pts/36 S+ 00:00:00 \_ sleep 5
Unfortunately this does not work for screen
as it sets the sid for each child screen and all grandchild bash.
To get all the processes spawned by a process the whole tree needs to be built. I used awk for that. At first it builds a hash array to contain all PID => ,child,child...
. At the end it calls a recursive function to extract all the child processes of a given process. The result is passed to another ps
to format the result. The actual PID has to be written as an argument to awk instead of <PID>
:
ps --forest $(ps -e --no-header -o pid,ppid|awk -vp=<PID> 'function r(s){print s;s=a[s];while(s){sub(",","",s);t=s;sub(",.*","",t);sub("[0-9]+","",s);r(t)}}{a[$2]=a[$2]","$1}END{r(p)}')
For a SCREEN process (pid=8041) the example output looks like this:
PID TTY STAT TIME COMMAND
8041 ? Ss 0:00 SCREEN
8042 pts/8 Ss 0:00 \_ /bin/bash
8092 pts/8 T 0:00 \_ vim test_arg test_server
12473 pts/8 T 0:00 \_ vim
12972 pts/8 T 0:00 \_ vim
The correct command is
jobs
If you wish to have more info (not always available), it is
jobs -l
If you wish to bring job number 3 to the foreground,
fg %3
If you have a stopped job, you can resume its execution in the background by means of
bg %3
As for the BSD-style output ps ax, which, BTW, I use too, it is not too informative: it will show your background process in status S (interruptible sleep) if it is waiting for an event (user input?), or R if it is running. But this will not single them out among the bevvy of other processes. You can indeed identify it by combining this info with user ownership and terminal of execution, but I never do it this way because it is too cumbersome.
Best Answer
This should work: