Moving a process from background to foreground

31.12.12
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 temp.sh
>cat temp.sh
#!/bin/sh
for i in 2 3 3 3 45
do
sleep 10
echo $i
done
This process will sleep for 10 seconds for every iteration and prints the value of $i.
If I run the process as below:
> ./temp.sh
2
3
3
3
45
>
If you see above ,you can observe that the script took 50 seconds to return to the terminal.
So better for us to run the process in the background.
> ./temp.sh &
[1] 20323
>
The number which is present in the 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 background processes that were stared by you.
>jobs
[1] + Running ./temp.sh
[2] - Running ./temp.sh
[3] Running ./temp.sh
[4] Running ./temp.sh
fg is the command to bring it back to the foreground as shown below.
>fg 1
Now press CTRL+c. As seen above I have ended the process and it no longer exists. Now if I again run the command jobs
>jobs
[2]  + Running                       ./temp.sh
[3]    Running                       ./temp.sh
[4]  - Running                       ./temp.sh
>

No comments:

Post a Comment