Tuesday, July 03, 2007

Whats your background?

As part of job control its possible to stop a process and continue it later.
So while you are running a program, you can type Ctrl-Z to suspend it , its like a pause in a media player

Then you can ask bash to continue the process in background by typing 'bg'.
Background processes can not read from terminal and if they try to they will be suspended(paused).

Here is quick test for this:

bash$ cat read.sh
#!/bin/bash

sleep 5
echo -n "Enter some text > "
read text
echo "You entered: $text"

Run the script and hit Ctrl-Z

bash$ ./read.sh

[1]+ Stopped ./read.sh

Now run this in background using bg command.

You are prompted to enter some text

bash$ bg
[1]+ ./read.sh &
bash$ Enter some text >

As soon as you enter some text and hit Enter you will see this

bash$ Enter some text >

[1]+ Stopped ./read.sh

Any more attempts to run this in backgroup will suspend the script.
Run fg to get the script to run in foreground to get out of this loop.

bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ bg
[1]+ ./read.sh &

[1]+ Stopped ./read.sh
bash$ fg
./read.sh
hi there
You entered: hi there

0 Comments:

Post a Comment

<< Home