Running Programs in the Background

You will usually use the utstat computers “interactively”, starting a command, and waiting just a short time for the response. Sometimes, however, you may need to do something that requires extensive calculations, taking minutes, hours, or even days. Such programs should be run using the nohup command described below, so that you can log out of your terminal without them being terminated. You will also need to know how to monitor background programs, and how to kill background programs if necessary.

nohup – Running a program in the background

You will usually want to run long programs in the background, rather than have them occupying a window, and keeping you logged in. This is accomplished with a command such as

nohup myprog arg1 arg2 &

This runs the program myprog, passing it the two arguments arg1 and arg2. The “&” at the end of the command line causes the program to run in the background (ie, you can immediately type another command in this window, without waiting for the program to finish).
You can run several programs with nohup, but as a courtesy to other users, you shouldn’t run more than two at once. We have only two processors on fisher and photon, so there’s no good reason for you to run more than two programs at the same time.

A program started with nohup will stay around after you log out. Any output from the program that would go to the window will instead be written to the file nohup.out. Some programs such as R have non-interactive modes that are designed for running things in the background. For example,

nohup R BATCH infile outfile &

will run S-Plus in the background with commands coming from infile and output going to outfile. For other programs, you can redirect output with the standard shell facilities (adding >outfile to the command line).

To run a MATLAB program in the background use the command,

nohup matlab -nojvm  </path/input.m> output.txt &

Any problems please contact me.

Dermot Whelan

Monitoring the programs you are running

When you are running a program in the background, or in another window, you can look at what it is doing using the ps command. This command gives information about “processes” (programs being run) that you or someone else has started. See “man ps” for all the details.

Most of the time, you will be interested only in your own processes. The myps command will list all your processes, the program they are running, and various information on how much time and memory they are using. Here’s an example:

utstat: myps

    USER   PID PRI NI SIZE(K)  RSS(K)  RUNTIME S  STARTED COMMAND
 radford 22442  59 20    3824    3184     0:02 S 19:38:20 /usr/openwin/bin/xterm radford 22441  59 20    2552     848     0:00 S 19:38:20 bash
 radford 24375  49 20    1080     880     0:00 O 20:44:57 /bin/ps
 radford 22443  59 20    7616    5928     0:03 S 19:38:20 emacs
 radford 16629  49 20    2552    2080     0:00 S 17:11:52 bash
 radford 16674  59 20   36344   29992     2:05 S 17:12:17 netscape
 radford 16675  59 20   20584    5544     0:00 S 17:12:17 (dns
 radford 24328   0 20  900928  784456    10:09 O 20:43:47 mtest

Here we see a number of processes that are involved with various windows that I (radford) created. One of these processes is running a program I wrote called mtest, which I started today at 20:43:46, using nohup. It has taken over 10 minutes of computation time, and is currently still “running” (as opposed to “sleeping”). This process is occupying 900928 Kilobyes of memory 784456 kilobytes are currently “resident” in main memory.
This mtest program is taking quite a bit of time and memory. If it’s doing something useful, that may be fine, but it could also be that there’s a problem with the program. If the program is using lots of memory, it may also be having too big an effect on other users.

The top command will display the processes (owned by anyone) that are presently doing the most computation. For each process, top shows the fraction of the processors that it has been using recently (as “CPU%”). To exit top, type “q”. There are two processors on fisher, so up to two programs can be running simultaneously. When more than two programs want to run, they will have to share the processors.

If you decide that a program you are running is doing the wrong thing, or is taking too much time or memory, you will need to kill it, as described below.

Killing programs

You can usually kill a program that you are running interactively by just typing Control/C (ie, hold down the Ctrl key and then press C).

If you are running a program in the background, you can kill it with the kill command. To do this, you need to know its “process id” (PID), which you can get using the myps commands (see above). For example, if I decided to kill the mtest program shown in the output of myps above, I would use the command

kill 24328

You can confirm that this worked by doing another myps and checking that the process no longer exists. Some processes won’t die this way, but you can kill them for sure by using the -KILL option before the process id.

Print Friendly