1833513 Members
2926 Online
110061 Solutions
New Discussion

Process type !

 
SOLVED
Go to solution
Fragon
Trusted Contributor

Process type !

How can I judge if a process is a background process or foreground process? (Suppose the process is run by others)
============================
For example "(sleep 20)&" is a background process, but "sleep 20" is foreground process. But I use "ps -ef |grep sleep" just get the same result.
============================
Thanks a lot.

-Gerald-

4 REPLIES 4
Ralph Grothe
Honored Contributor

Re: Process type !

I assume you want to identify background processes from the process table.
The PPID of your sleep command should be the PID of the shell that forked the sleep.
But this doesn't really help much.
It is easy though to identify background processes from the shell (or script) that is the parent.
A

ps -fp $(echo $!)

should show the process.
But you'd better use the jobs command (provided your shell has "set -o monitor").
"jobs -l"
gives information including the backgrounded jobs' PIDs.
Madness, thy name is system administration
Ceesjan van Hattum
Esteemed Contributor

Re: Process type !

A background process is still connected to its parent and still connected to its TTY.
It will give priority to foreground jobs and it will return your command-prompt, nothing more.

Use "jobs -l":
$> (sleep 2000)&
[1] + 17391 Running (sleep 2000)&
$> jobs
[1] + Running (sleep 2000)&
$> jobs -l
[1] + 17391 Running (sleep 2000)&
$> kill 17391
[1] + Terminated (sleep 2000)&
$> ps -ef | grep 17391
$> .. not found

Regards,
Ceesjan

Thomas Schler_1
Trusted Contributor
Solution

Re: Process type !

Gerald,

I'm looking for the nice value each job is running with. The standard nice value is 20 normally used for interactive jobs run by users. If a user puts a job into the background the nice value is higher. Using ksh the nice value is 24 for background jobs, using csh I think it is 22 (instead of 20).

The nice values can be seen with 'ps -l' or 'top' in column NI.

But, higher nice values are just a hint that the associated job is really a background job. Users can set the nice value by their own, but, I think only very few are aware of the nice(1) command and use it, regularly. The nice command was more familiar 10 or 20 years ago, when users run huge jobs in the background but having had comparatively poor resources. So, their jobs had to be very "nice" to interactive jobs or small jobs of other users.
no users -- no problems
Ceesjan van Hattum
Esteemed Contributor

Re: Process type !

thanks for the points, gerald.
Nevertheless, you do not seem to be completely satisfied on our answers to your question.
Please explain what you want to know else then these answers?