-->
Previous Table of Contents Next


In this example, the -t option is used to restrict the listing to the processes associated with terminals tty01 and tty02. Terminal tty02 is running the shell command (PID 32) and using vi to edit the calendar (PID 235). The cumulative time for each process is also listed. If you’re using shells from a graphical interface (the xterm command), use device names pts001, pts002, and so on with the -t option to see the processes from those sessions.

Sometimes a process is marked as <defunct>, which means that the process has terminated and its parent process has been notified, but the parent hasn’t acknowledged that the process is “dead.” A process like that is called a zombie process. It’s possible that the parent is busy with something else and the zombie will soon disappear. If you see a number of defunct processes or ones that linger for some time, this is a sign of some difficulty with the operating system.


NOTE:  Because a zombie process has no parent, you can’t kill the zombie. The only way to get rid of a zombie process is to reboot your machine.

Controlling Multiple Processes

Linux gives you the power to run several processes concurrently. It also allows a user or an administrator to have control over running processes. This control is advantageous when you need to do the following:

  Initiate a process that continues after its parent quits running (use the nohup command)
  Schedule a process with a priority different than other processes (use the nice command)
  Terminate or stop a process (use the kill command)

Using nohup with Background Processes

Normally, the children of a process terminate when the parent dies or terminates. This means that when you start a background process, it terminates when you log out. To have a process continue after you log out, use the nohup command. Put nohup at the beginning of a command line:


nohup sort sales.dat &

This sample command tells the sort command to ignore the fact that you log out of the system; it should run until the process completes. In this way, you can initiate a process that can run for days or even weeks. What’s more, you don’t have to be logged in as it runs. Naturally, you want to make sure that the job you initiate behaves nicely—that is, it eventually terminates and doesn’t create an excessive amount of output.

When you use nohup, the command sends all the output and error messages of a command that normally appear onscreen to a file named nohup.out. Consider the following example:


$  nohup sort sales.dat &

1252

Sending output to nohup.out

$

The sorted file and any error messages are placed in the file nohup.out. Now consider this example:


$  nohup sort sales.dat > sales.srt &

1257

Sending output to nohup.out

$

Any error messages are placed in the nohup.out file, but the sorted sales.dat file is placed in sales.srt.


NOTE:  When you use nohup with a pipeline, you must use nohup with each command in the pipeline:

nohup sort sales.dat | nohup mailx -s“Sorted Sales Data” boss &


Scheduling the Priority of Commands with nice

Use the nice command to run a command at a specific scheduling priority. The nice command gives you some control over the priority of one job over another. If you don’t use nice, processes run at a set priority. You can lower the priority of a process with the nice command so that other processes can be scheduled to use the CPU more frequently than the nice job. The superuser (the person who can log in as the root user) can also raise the priority of a process.


NOTE:  The commands nice –help and nice –version don’t work in the GNU implementation of nice.

The general form of the nice command is as follows:


nice -number command

The priority level is determined by the number argument (a higher number means a lower priority). The default is set to 10, and number is an offset to the default. If the number argument is present, the priority is incremented by that amount up to a limit of 20. If you enter the following command, the sort process starts with a priority of 10:


sort sales.dat > sales.srt &

If you want to start another process—say, with the lp command—but give preference to the sort command, you can enter the following:


nice -5 lp mail_list &

To give the lp command the lowest possible priority, enter this:


nice -10 lp mail_list &


NOTE:  The number flag above is preceded by the flag specifier -, which you shouldn’t confuse with the negative number sign.

Only superusers can increase the priority of a process. To do that, they use a negative number as the argument to nice. Remember—the lower the nice value, the higher the priority (up to a maximum priority of 20). To give a job “top priority,” a superuser initiates the job as follows:


nice –10 job &

The ampersand (&) is optional; if job is interactive, you wouldn’t use the ampersand to place the process in the background.

Scheduling the Priority of Running Processes with renice

The renice command, available on some systems, allows you to modify the priority of a running process. Berkeley UNIX systems have the renice command; it’s also available in the /usr/ucb directory in Linux System V systems for compatibility with Berkeley systems. With renice, you can adjust priorities on commands as they execute. The format of renice is similar to that of nice:


renice -number  PID

To change the priority on a running process, you must know its PID. To find the PID of all your processes, enter this command:


ps -e | grep  name

In this command, name represents the name of the running process. The grep command filters out all processes that don’t contain the name of the process you’re looking for. If several processes of that name are running, you have to determine the one you want by looking at the time it started. If you want to affect all processes belonging to a certain group or a certain user, you can specify the GID or UID of the running processes to the renice command.


Previous Table of Contents Next