How can I know if a process is running background or foreground in commands ps or ps -ef?

I first thought that when I type in ps -ef, there is a section named TTY. when I look into the list of TTY, most of them are ?. I wonder if that question mark means process running background.

I want to check if I am right! If not, I want to know how I can know if the process is running background or foreground?

ps. command jobs is not used here.

2 Answers 2


The stat field has an extra + for foreground processes:

ps -e -o pid,tty,stat,cmd

Foreground processes are those whose process group id (pgid) is the foreground one on the terminal (tpgid):

ps -e -o pid,pgid,tpgid,tty,cmd | awk '$2==$3'

when I look into the list of TTY, most of them are '?' (question marks) I wonder if that question mark means process running background

No, that means that they have no controlling tty (daemons or kernel threads, neither foreground nor background). For a list of background proceses:

ps -e -o pid,pgid,tpgid,tty,cmd | awk '$3!=-1 && $2!=$3'

The OP asked, "... how I can know if the process is running background or foreground?"

The foreground and background status of a process are reported by ps as the state of the process. man ps lists these process states under the heading of PROCESS STATE CODE.

ps -ef doesn't report process state, but referring to man ps we find in the OUTPUT FORMAT CONTROL section that the -o argument may be used to specify the output of ps via one or more keywords. These keywords are listed in the STANDARD FORMAT SPECIFIERS section, and with extraordinary persistence one will eventually find the s, stat and state keywords listed. But note that while all three of these keywords will provide state in the output, only the stat keyword will give the multi-character process state! The take-away: ps favors the diligent user.

As an example, the following ps command will output the PID, the state, and the command that started the process using the keywords pid, stat and command:

ps -eo pid,stat,command

To see how this works, consider this example:

In one terminal, run this command:

ping > /dev/null

In another terminal:

ps -eo pid,stat,command | grep ping | grep -v grep
12518 S+   ping

Which informs us:

  • the PID is 12518
  • the process state is S+ (via keyword stat)
  • the command that spawned the process was ping

There is one more step required to learn if this process if foreground or background: The state value of S+ must be 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

Consequently, we see from the + in the second character position that this process is running in the foreground. A background process may be listed with an S in the first character position, and nothing in the second position. There are several other character combinations that indicate a background process; see this for a listing.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .