Academic Computing Services * Simon Fraser University

HOW TO

Use the Unix clock

© July 30, 1996 B-11



Unix keeps track of time internally using its system clock. Because of this, you can

Finding the current date and time

The simplest way to use the Unix clock is to ask for the date and time. The command date shows the current date and time for your time zone.

date

Tue Jul 30 10:38:11 PDT 1996

(PDT stands for Pacific Daylight Time.)

Displaying a calendar

The command cal will create and display a calendar for a particular month or a whole year. If you don't specify a particular month or year, the current month's calendar is shown. Typing cal produces

      July 1996



 S  M Tu  W Th  F  S

    1  2  3  4  5  6

 7  8  9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

To see a calendar for October 1867, type

cal 10 1867

The result is:

   October 1867 



 S  M Tu  W Th  F  S

       1  2  3  4  5

 6  7  8  9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

Timing commands

The command time does not give time of day, but rather measures how long it takes for a command to be executed. For example, to time how long it takes to list all files and the contents of all directories (using ls -R), type

time ls -R

The response will be a complete recursive listing of all files and directories, followed by

0.1u 0.3s 0:03 13%

The first three figures show, in seconds:
user timetime spent executing the command (0.1u in the example above)
system timetime spent in the system (0.3s above)
elapsed timetotal elapsed time from when you first gave the command until the command was completed (0:03 above).

The fourth figure (13% above) is the CPU time (user plus system time) as a percentage of the elapsed time.

As demonstrated in the example, the sum of the CPU times can be larger than the elapsed time. This is because elapsed time is accurate to the second, while the CPU times are measured to the 50th second.

Running commands at a later time

You may have tasks which must be performed at a certain time, or which are computing-intensive and should be postponed until the system load is low. The at, batch and nice commands will help you accomplish these things.

The at command lets you put together a list of commands and tell Unix when to run them. The batch command is similar, but instead of running the commands at a specific time, batch waits only until the system load is low enough before running the commands. The nice command will run very computing-intensive jobs at a lower priority than normal.

The at command

The at command allows you to specify commands that you want to run at a later time. Suppose that you want to know who is logged in at 11:30 pm this evening, long after you've left. Type at, followed by the time at which you want the job to be run.

at 11:30pm

Now type the command you want to run at that time:

who > nightowls

Type Control-d (hold down Control key and type d) to indicate you're finished typing commands to be run later. Unix will respond with confirmation and a job number:

job 838748580.a at Tue Jul 30 10:43:00 1996

At 11:30 the Unix system will run the who command and put the results in a file called nightowls.

at normally takes its input from the keyboard (i.e., you have to type the commands you want run later). However, it is possible to put a series of commands into a file and redirect that file to the at command. For example, you could create a file called snoopy containing the commands you want to run:

cat > snoopy
who > nightowls
w > activity
^D (type Control-d)

To make these commands run at 11:30pm, type

at 11:30pm < snoopy

Instead of waiting to take its input from the keyboard, the at command will use the commands in the file snoopy as its input (the < symbol indicates redirection of input). At 11:30 (or a bit later), Unix will run all the commands in the file snoopy. In this case, it will run the who command and put the results in a file called nightowls, then it will run the w command and put the results in a file called activity. (For more on the who and w commands, see how-to B-9, Find out about others using Unix and "talk" to them.)

The at command understands time in several formats:

at 9:00am
at 5 pm Friday
at 0815am Jan 24
at now + 12 hours

If no day or date is given, today is assumed. Days of the week or dates in a month can be used. at can also count from the current time ("now").

To schedule commands to be regularly run at periodic intervals, use the crontab utility. For information about crontab, consult a Unix reference guide or type man crontab.

The batch command

If you have a number of long files to be counted and sorted, you might use the batch command so as not to overload the system. batch waits until the system load is "low enough" (as defined by the System Administrator), then runs the commands.

Begin using batch by typing

batch

Now you may type all the commands you want to run, one per line, for example:

wc essay > essaylength
sort list > sortedlist

When you are finished, type Control-d. Unix will respond with a job number.

When the system load is low enough, your commands will be run. In this case, the results of the wc (word count) command will be in a file called essaylength, and the results of the sort command will be in a file called sortedlist. (See how-to B-18, Sort, Count and Compare Unix Files, for more on the wc and sort commands.)

The nice command

If you have very computing-intensive work to do (for example, calculating the value of pi to one million decimal places), running your job may have a deleterious impact on the computer's responsiveness to other users. You can prevent this by being nice and using the nice command.

Although it may appear to each user of a multi-user system that the computer is working exclusively for her/him, in fact it allocates a time slice to each user's job and switches continuously from processing one job to processing the next. Using the nice command gives a shorter than normal time slice to your job.

To run your calculation (the commands for which are in a file called calcpi), type

nice calcpi &

The & (ampersand) runs your job calcpi in the background, allowing you to continue with other computing tasks. For more information on running multiple jobs simultaneously, see how-to B-15, Use Job Control with the Unix C-shell.

If your job calcpi is not likely to be finished before you intend to logout, add the nohup ("nohangup") command:

nice nohup calcpi &

Note that computing-intensive jobs should not be run on Fraser, the general-purpose Unix computer. See how-to A-3, Understand the Campus Network of Unix Computers, for information on appropriate Unix hosts for computing-intensive jobs.


* * * * * * * * * * * * * * *

This page written and maintained by Academic Computing Services, Simon Fraser University.
Please e-mail questions or comments to help@sfu.ca.