Bama Behind the Scenes
(Editor's Note: Bama has been through multiple upgrades since this article was written.)
Bama, being an eight-processor machine with two Gigabytes of virtual memory, can run hundreds of processes at the same time. It keeps them going in a process table as they wait their turn to run on a CPU. Some of these processes are just users who are logged in and running a program or they may be people getting mail through IMAP or POP. Other processes are system jobs that are continuously going on behind the scenes. Anyone can look at the entire process table. Try the command:
ps -eaf | page
The output of "ps" has been piped through "page" since it is a very, very long list of processes.
You will see each process owner's name in the left-hand column. The jobs that belong to root are the system jobs. Then there are jobs being run by users. There may be several processes associated with a single user.
It is also possible for a user to have a job running even though they are not logged in. These jobs are known as batch jobs or background jobs (or occasionally, those that aren't supposed to hanging around are "hung" jobs). Any user who has a job that must run for a long period of time, longer than they plan to stay logged in, for instance, may put the job in the background. There are several ways to do this.
If the job is ready to be run without any input from the user it can be put directly into the background with
nohup command &
where command is the name of the program to be run. The "nohup" is there to keep the job running even after the user logs out. It is not necessary to use this if you use the csh. Once started, the status of this background job can be checked with the command "jobs". On subsequent logins, however, "jobs" will not show the background jobs; "ps" must be used instead. The output of the "jobs" command looks like this:
[1] + Running command &
where command is the command you ran. If you have logged out and in again and need to use "ps", use this form:
ps -fu userid
where userid is your user id (also called account name).
It turns out that jobs that are run in the background are not stuck there. Likewise, interactive commands (or foreground commands) are not stuck there either. Provided you are working in the same login session (i.e. you do not log out then in again) you can move jobs from the foreground to the background and visa versa.
Why would you want to do this? Well, suppose there is a job you start in the foreground that takes longer than you expected. You don't want to be stuck waiting for it to finish. You could go off and start another login but a quicker way to handle this situation is to put the job in the background. The opposite case might be where you start a job in the background and you discover that it wanted you to give it some input from the keyboard. You can't do this to a background job. You must bring it to the foreground so you can answer the program's question.
To put a job in the background first you must stop the job with a Control-z (press the control key and the z key at the same time). You will see this on your screen
^Z[1] + Stopped (SIGTSTP) command
where command is the command you were running. The job is still there; it is just paused. To put it in the background you simply type
bg
A background job which is waiting for input will generate this line
[1] + Stopped (SIGTTIN) command &
where command is the command you put in the background. Bama won't give you this message immediately, so as to not interrupt what you are doing. It will wait until just before it prints out a prompt. To bring the job to the foreground, simply type
fg
A job does not have to be stopped for you to bring it to the foreground. However, unless a job is stopped and waiting for input, there usually isn't a reason to bring it to the foreground.
If you have created more than one background job you may have to tell Bama which job to bring to the foreground. In this case, "jobs" will show each job with a number preceding it. You can pick the jobs you want to bring to the foreground with
fg %n
where n is the job number. Once the job is in the foreground you can answer its question(s) and even put it back in the background using the technique just reference, if you like.
Generally, you will be warned before you log out if you have jobs in the background. If they were there unintentionally, such as stopped jobs, you can remove them. In fact, the first time you try to log out when you have background jobs, you will be prevented for doing so. If you issue the exit command a second time, it will let you log out. At this point, background jobs that do not have "nohup" will be terminated. To kill a job which is in the background (or is stopped) type
kill %n
where n is the number of the job.
There is a complete man page on "bg" and "fg". Note that there are other ways to refer to the jobs besides its number. These techniques are outlined in the man page. Be somewhat careful in reading this information. It is the combined job control information for the different shells. The "notify" command it lists, for instance, is only valid in the csh. Remember that the default shell for all users on Bama is the ksh.
© 1998, The University of Alabama. The information included here is for the University of Alabama central computing facility as it was configured on the document date. It may or may not apply to other Unix systems.

