Monday, December 31, 2012

Running a background process and foreground process

In unix, if we run a process(a shell script).It will not return to the terminal untill the script ends.
lets say there is a script
for i in 2 3 3 3 45
sleep 10
echo $i
This process will sleep for 10 seconds for every iteration and prints the value opf $i.
if i run the process as below:
> ./
 If you see above ,you can observer that the script took 50 seconds to return to the terminal.
so better for us to run the process in the how do you do it?see below:
> ./ &
[1] 20323
The number which is present in teh square braces is the job id and the number outside the square braces is the process id(pid)
Now if you see ps output you can see the process as running or also in the output of jobs
Yes,the command jobs will return all the backgropund processes that were stared by you.
[1] + Running ./
[2] - Running ./
[3] Running ./
[4] Running ./
fg is the command to bring it back to the foreground as shown below.
>fg 1
[CTRL -c]
As seen above i have ended the process and it no longer exists. now if i again run jobs
[2]  + Running                       ./
[3]    Running                       ./
[4]  - Running                       ./

No comments:

Post a Comment