3

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.

5

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'
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.