Flylib.com

Books Software

 
 
 

Timing Jobs with time


Timing Jobs with time

Sometimes, you might want to know how long a job takes to complete. You can do so using the time command, which times jobs according to the built-in Unix timer. As Code Listing 9.10 shows, all you have to do is enter time followed by the command you want to time.

Code Listing 9.10. Enter time plus the full job command to find out the job time.
[ejr@hobbes running]$

time slowscript

0.05user 0.06system 0:50.12elapsed 0%CPU
[ejr@hobbes running]$

time ls

bigdog.tar.gz  slowscript testing.gif
0.03user 0.00system 0:00.03elapsed 78%CPU
[ejr@hobbes running]$

time nice -19 ls

bigdog.tar.gz  slowscript testing.gif
0.03user 0.03system 0:00.06elapsed 93%CPU
[ejr@hobbes running]$

time -p nice -n 19 ls

foo
real 0.00
user 0.00
sys 0.00
[ejr@hobbes running]$

To Time a Job Using time :

  • time slowscript

    At the shell prompt, type time followed by the complete command. After the command finishes, the system will tell you how long it took, as shown in Code Listing 9.10.

To Compare Job Times with time :

1.

time ls /usr

At the shell prompt, type time followed by a job (here, ls ).

2.

time nice -n 19 ls /usr

Then, type time followed by another job. In this example, we're comparing a regular ls command to a nice ls command. As Code Listing 9.10 shows, the elapsed time for the nice ls command was considerably longer than the regular ls command.

Tips

  • Keep in mind that the time a job takes to run may vary according to the system's current load or capacity. For example, a job might take less time to run at 2a.m., when few people are using the Unix system, compared with 2 p.m., when many more people are using the system.

  • Different systems produce slightly different time outputs. On some systems, you'll get real (clock) time, user time, and system time. Real time is how many seconds on the clock elapsed while the program was running, while user and system time both refer to different measures of how long it took the system to run the job. On other systems, you might get a ton of garbage, as shown in Code Listing 9.11 , but the gist of the information is the same.

  • In the case of the ls example, you're really not concerned with either the output or the errorsjust the timeso you can creatively dispense with all of it. Try time ls /usr > /dev/null 2>&1 to send standard output to /dev/null (the bitbucket) and send error messages to standard output (and thence to the bitbucket). See Chapter 17 for details.

  • Try time p to get a more human-readable output.

    Code Listing 9.11. time output varies from system to system. Here, we get a bunch of garbage to decipher in addition to the time information.
    $
    
    time slowscript
    
    0.07user 0.05system 0:50.13elapsed 0%CPU
    (0avgtext+0avgdata 0maxresident)k
    0inputs+0outputs
    
    (219major+59minor)pagefaults 0swaps
    $
    




Finding Out What Processes are Running with ps

Code Listing 9.12. Using ps , you can find out what processes are currently running.
[jdoe@frazz jdoe]$

ps

PID TTY      TIME CMD
21016 pts/22  00:00:00 bash
21707 pts/22  00:00:00 ps
[jdoe@frazz jdoe]$

ps -a

PID TTY     TIME CMD
21407 pts/3   00:00:00 su
21411 pts/3   00:00:00 bash
21441 pts/3   00:00:00 su
21444 pts/3   00:00:00 bash
19274 pts/11  00:00:05 xterm
23357 pts/12  00:00:04 xterm
13369 pts/5   00:00:00 zsh
23815 pts/9   00:00:00 su
23818 pts/9   00:00:00 bash
23878 pts/9   00:00:00 csh
23942 pts/9   00:00:01 ssh
23972 pts/18  00:00:00 su
23975 pts/18  00:00:00 bash
24103 pts/5   00:00:00 ssh
 4658 pts/15  00:00:11 ssh
24318 pts/8   00:00:01 xterm
29188 pts/4   00:00:00 rxvt-2.7.9
29368 pts/4   00:00:00 rxvt
29440 pts/8   00:00:00 vi
23883 pts/20  00:00:02 xterm
27257 pts/16  00:00:01 ssh
 6004 pts/20  00:00:00 xterm
20531 pts/20  00:00:02 xterm
21013 pts/22  00:00:00 su
21016 pts/22  00:00:00 bash
21708 pts/22  00:00:00 ps
[jdoe@frazz jdoe]$

The jobs that we've been talking about so far are actually types of processes. Processes are programs, scripts, or commandsincluding anything you do in the Unix system. All jobs are processes, but not all processes are jobs.

Occasionally, you may want to find out what processes are running on the Unix system. You can do this using ps , as shown in Code Listing 9.12 .

To Find Out What Processes are Running with ps :

  • ps

    At the shell prompt, type ps to see the list of the current processes that you're running in your current shell, including processes for your current shell, as well as any other jobs (Code Listing 9.12). The exact information you see will vary from system to system. In general, though, you'll find the PID (process identification) number at the far left and the process name at the right.

Tips

  • You can find out what processes other people are running by typing ps -a at the shell prompt and what processes the system is running (also called daemons ) with ps -ax . The ps ef variant is usually pretty useful for us.

  • You can sometimes, depending on the system, get a broader look at currently running processes by typing ps a f . The f flag indicates "forest" view, which lets you see not only the processes, but also how they relate to each other, as shown in Code Listing 9.13 .

  • The results ps offers vary greatly depending on the Unix flavor you're using. Type man ps at the shell prompt to find out more about your specific ps capabilities.

    Code Listing 9.13. The forest view gives you a broader look at running processes.
    $
    
    ps a f
    
    PID TTY STAT TIME COMMAND
    15043 p0 S 0:00 /bin/login -h calvin raycomm.com -p
    15044 p0 S 0:01  \_ -bash
    16344 p0 T N 0:00       \_ sh ./slowscript
    16345 p0 T N 0:00         \_ sleep 50
    16449 p0 R 0:00       \_ ps f
    15911 p1 S 0:00 /bin/login -h calvin raycomm.com -p
    15914 p1 S 0:01  \_ -bash
    16216 p1 T 0:00      \_ telnet
    16217 p1 T 0:00      \_ lynx http://www.raycomm.com/
    16267 p1 T 0:00      \_ man telnet
    16268 p1 T 0:00         \_ sh -c (cd /usr/man ; (echo -e ".pl 1100i"; cat /
    16269 p1 T 0:00             \_ sh -c (cd /usr/man ; (echo -e ".pl 1100i"; c
    16270 p1 T 0:00                 \_ sh -c (cd /usr/man ; (echo -e ".pl 1100i
    16272 p1 T 0:00                  \_ cat /usr/man/man1/telnet.1
    16271 p1 T 0:00                 \_ /usr/bin/gtbl
    16273 p1 T 0:00                 \_ sh -c (cd /usr/man ; (echo -e ".pl 1100i
    $