Guide to Unix/Explanations/Pipes and Job Control

From Wikibooks, open books for an open world
< Guide to Unix | Explanations
Jump to: navigation, search

To do:
todonote

[edit] Pipes and Redirects

We can connect standard input, output, and error of commands to other commands or to files.

[edit] Job Control

You can run multiple commands at once, but only one can be in the foreground. The other commands can be in running at the background or suspended.

Create the shell script "date_loop.sh"

#!/bin/bash
while :
do
    date > datefile
    sleep 10
done

Make date_loop.sh executable:

$ chmod +x date_loop.sh

Run the command in foreground:

$ ./date_loop.sh

Wait a while (about 30 seconds) then use CONTROL-Z to send SIGTSTP signal to the process:

^Z[2] + Stopped (SIGTSTP)        date_loop

Look at the last line of datefile:

$ tail -1 datefile; sleep 10; tail -1 datefile
Thu Apr 27 10:46:06 BST 2006
Thu Apr 27 10:46:06 BST 2006

Note, that while the process was runnable, it was suspended, thus new "times" were not appended to the file in the 10 second interval between respective tail commands.

Put the process into background:

 
$ bg
[2]     date_loop.sh&

The process is resumed and datefile is being updated again:

$ tail -1 datefile; sleep 10 ; tail -1 datefile
Thu Apr 27 10:48:34 BST 2006
Thu Apr 27 10:58:54 BST 2006

Note that you can type commands at the command prompt.

Put the process back into foreground:

$ fg
date_loop.sh

Send a 'SIGINT to the process by typing "control-C". This terminates the process. We confirm that datefile is not being updated anymore:

^C
$ tail -1 datefile; sleep 20; tail -1 datefile
Thu Apr 27 10:59:55 BST 2006
Thu Apr 27 10:59:55 BST 2006


Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export