8

Is it possible to list all running background processes with the ps command, or is the only option for getting a list of background processes the jobs command?

2 Answers 2

9

The jobs command will show any background jobs started within the current shell, usually by starting a background task with the & operator or ^Z bg (e.g. sleep 10 &).

If you want to see all of the background processes running on the system, you can use ps -e, or ps -eF to get some additional details.

To get more information about what commands are able to do, you can run almost always run man ps (replacing ps with any command) to get the manual page with lots of info.

7

"background processes" usually refers to terminal job control. That would be processes that are in process groups that are not the foreground process group of their controlling terminal device.

On Linux systems with the procps implementation of ps, you can find them with:

ps -eo pid,pgid,tpgid,args | awk 'NR == 1 || ($3 != -1 && $2 != $3)'

Where we look for processes where tpgid (the terminal foreground process group id) is not -1 (the processes that have a controlling terminal) and where their process group id is not the tpgid.

Note that it will also include the shells that are currently busy waiting for a foreground job (including the one where you run that pipeline from if run in foreground in a terminal) as they will have put those jobs in foreground and will be therefore by definition themselves in background.

You must log in to answer this question.

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