# We will first learn how to run a process in the background and how # to get its process ID (pid). The first step is running a command # without waiting for the result, which is achieved by ending the # command with a single ampersand, like this: while true; do sleep 1; echo .; done & # Now the entire compound command is running in a separate process # (the shell has forked): the parent processes continues to execute # this script (i.e. the commands below) while the child runs the # command above (it runs in the background, but it can still print # to the terminal). To do anything useful with the running process, # we need to learn its process identifier, which is stored in the # shell variable ‹$!›. child=$! echo $child # Then we wait a bit, so that the loop above can print a few lines. # The ‹sleep› command, as you probably figured out by now, does # nothing for a given number of seconds (5 in this case). sleep 5 # Now we request that the child process terminates. This is a ‘nice’ # request, in the sense that the operating system notifies the # process that the user asked for its termination. The process may # or may not honour the request (it usually will, but it may perhaps # try to clean things up before terminating). kill $child # As usual, you can look up ‹kill› in the manual. That's it, we will # look at a few more process management commands now: # # $ micro ps.sh