8

I can see the background process using ps. But Is there a way to view the foreground process? For example,

$nohup process1 &

then

ps -ef | grep "process1"

would display the process "process1" in execution. But the above command wouldn't show a foreground process executed like,

$process2

4
  • 1
    ps -ef shows all foreground processes for me. Which OS are you running on?
    – bahamat
    Jan 19, 2011 at 19:49
  • 2
    Are you sure process2 is still running when you typing 'ps'? Maybe it's already done. Mar 19, 2011 at 6:33
  • By the time you get the prompt back, the foreground process is done.
    – vonbrand
    Mar 14, 2013 at 1:15
  • I feel this question is misleading; see the answer below for an explanation.
    – Seamus
    Dec 13, 2021 at 6:02

3 Answers 3

5

You might be confused because ps by default shows you the processes which are on the same terminal where ps is invoked, e.g. processes started from the same terminal window.

Try ps -u $LOGNAME or if you know the terminal names ps -t $THETTYNAME1,$THETTYNAME2. (The terminal names normally look like "ttyN" or "pts/N").

2
  • ps - ef shows all processes, not just ones on "this" terminal.
    – bahamat
    Jan 19, 2011 at 19:51
  • This answer is incorrect - or perhaps 'insufficiently qualified'. On my macOS (which uses a ps with a BSD heritage), issuing the ps command with no options lists processes on all 6 terminals (ttysxxx). man ps yields: The ps utility displays a header line, followed by lines containing information about all of your processes that have controlling terminals.
    – Seamus
    Dec 11, 2021 at 17:39
1

Weird, the ps command allows you to monitor the status of all active processes, both foreground and background.

For example, I start the following process in a first shell:

$ startNetworkServer 
Security manager installed using the Basic server security policy.
Apache Derby Network Server - 10.3.2.1 - (599110) started and ready to accept connections on port 1527 at 2010-06-18 14:14:33.277 GMT 

And in another shell:

$ ps aux  | grep NetworkServer | grep -v grep
pascal    5928  0.3  1.4 661492 29216 pts/0    Sl+  16:14   0:03 /usr/lib/jvm/java-6-sun/bin/java -classpath /usr/share/javadb/lib/derby.jar:/usr/share/javadb/lib/derbynet.jar:/usr/share/javadb/lib/derbytools.jar:/usr/share/javadb/lib/derbyclient.jar org.apache.derby.drda.NetworkServerControl start

The process is listed as expected (and the + shows that it is in the foreground process group).

0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy