The question:
"Is there a way to view the foreground process?" The question then goes on to claim that ps -ef
does not show processes running in the foreground.
Answer:
Yes, there is a way. Also: ps -ef
does show processes running in the foreground - but it does not show the state of the processes. This is explained in the documentation (man ps
), and may be verified in a simple experiment. Consider this example:
In one terminal, run:
ping 8.8.8.8 > /dev/null
In another terminal:
ps -ef | grep ping | grep -v grep
seamus 1161 974 0 23:20 pts/1 00:00:00 ping 8.8.8.8
Note that ps -ef
does not provide state information in its output. However, man ps
explains that state information is available using the -o
argument when comined with the keyword stat
:
ps -eo pid,stat,command | grep ping | grep -v grep
1161 S+ ping 8.8.8.8
Which tells us:
- the process PID is
1161
via keyword pid
- the process state is
S+
via keyword stat
- the
command
keyword shows ping 8.8.8.8
spawned the process
The state value of S+
is decoded in man ps
under the heading of PROCESS STATE CODE
. which tells us:
S interruptible sleep (waiting for an event to complete)
+ is in the foreground process group
QED
We have seen that ps -ef
lists the process whose PID is 1161
, and that ps -eo pid,stat,command
reveals the state of PID 1161
to be a foreground process.
NOTE 1: keywords for the -o
argument are described in man ps
under the heading STANDARD FORMAT SPECIFIERS
. It is explained here that keyword stat
yields the two-character process state, while keyword state
yields only the one-character state.
NOTE 2: This works on my Debian-based system (reported as version ps from procps-ng 3.3.15
), and in macOS 10.15 (which is descended from the BSD version of ps
).
ps -ef
shows all foreground processes for me. Which OS are you running on?