Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [197]

By Root 985 0
of it in a timestamped directory tree that can be used to recover from changes made since the snapshot.

Process Accounting

Unix systems support process accounting, although it is often disabled to reduce the administrative log-file management burden. When it is enabled, on completion of each process, the kernel writes a compact binary record in a system-dependent accounting file, such as /var/adm/pacct or /var/account/pacct. The accounting file requires further processing before it can be turned into a text stream that is amenable to processing with standard tools. For example, on Sun Solaris, root might do something like this to produce a human-readable listing:

# acctcom -a

List accounting records

...

COMMAND START END REAL CPU MEAN

NAME USER TTYNAME TIME TIME (SECS) (SECS) SIZE(K)

...

cat jones ? 21:33:38 21:33:38 0.07 0.04 1046.00

echo jones ? 21:33:38 21:33:38 0.13 0.04 884.00

make jones ? 21:33:38 21:33:38 0.53 0.05 1048.00

grep jones ? 21:33:38 21:33:38 0.14 0.03 840.00

bash jones ? 21:33:38 21:33:38 0.55 0.02 1592.00

....

Because the output format and the accounting tools differ between Unix implementations, we cannot provide portable scripts for summarizing accounting data. However, the sample output shows that the text format is relatively simple. For example, we can easily produce a list of the top ten commands and their usage counts like this:

# acctcom -a | cut -d ' ' -f 1 | sort | uniq -c | sort -k1nr -k2 | head -n 10

21129 bash

5538 cat

4669 rm

3538 sed

1713 acomp

1378 cc

1252 cg

1252 iropt

1172 uname

808 gawk

Here, we used cut to extract the first field, then ordered that list with sort, reduced it to counts of duplicates with uniq, sorted that by descending count, and finally used head to display the first tenrecords in the list.

Use the command apropos accounting to identify accounting commands on your system. Common ones are acctcom, lastcomm, and sa: most have options to help reduce the voluminous log data to manageable reports.

Delayed Scheduling of Processes

In most cases, users want processes to start immediately and finish quickly. The shell therefore normally starts each command as soon as the previous one finishes. Command completion speed is essentially resource-limited, and beyond the shell's purview.

In interactive use, it is sometimes unnecessary to wait for one command to complete before starting another. This is so common that the shell provides a simple way to request it: any command that ends with an ampersand is started in the background, but not waited for. In those rare cases in which you need to wait for backgrounded processes to complete, simply issue the wait command, as described in Section 13.2.

There are at least four other situations when it is desirable to delay process start until a future time; we treat them in the following subsections.

sleep: Delay Awhile

When a process should not be started until a certain time period has elapsed, use the sleep command to suspend execution for a specified number of seconds, then issue the delayed command. The sleep command uses few resources, and can be used without causing interference with active processes: indeed, the scheduler simply ignores the sleeping process until it finally awakes when its timer expires.

We use a short sleep in Example 13-1 and Example 13-3 to create programs that have an infinite loop, but do not consume all of the machine's resources in doing so. The short sleep in Section 9.10, ensures that a new pseudorandom-number generator seed is selected for each process in a loop. The long sleep in Section 13.3 waits until a more convenient time to resume a suspended resource-hungry job.

Most daemons do their work, and then sleep for a short while before waking to check for more work; that way, they consume few resources and run with little effect on other processes for as long as the system is operational. They usually invoke the sleep() or usleep( ) functions,[5] instead of using the sleep command directly, unless they are themselves shell scripts.

at:

Return Main Page Previous Page Next Page

®Online Book Reader