time command is a basic tool in Unix which allows you to keep track of the system resources when running a specified Unix command.
time command in Unix
Sometimes it is quite important to know exactly how much of your system resources are used for running a particular command. This is where the time command can be used. It's a really simple tool which takes any command line as a parameter, runs the command and then reports the system resources usage:
ubuntu# time du -sk /var 4228720 /var real 0m17.747s user 0m0.010s sys 0m0.080s
In this example, a du command is run to gather the cumulative disk space taken up by the /var directory, and a report of used time is presented.
This is what each of these times mean:
- real – real time, in other words a number of seconds, minutes and sometimes hours and even days it takes for the specified command to complete
- user -user time, that is the time spent by your OS executing the user code of your command – every instruction of the specified command which was executed in user mode.
- sys – system time, the amount of time spent by your OS running a system kernel code – instructions in response to the system calls initiated by your command
Real time vs user time vs system time
As you can see, the real time is not a sum of the other two – this is because only the system resources are reported, which is essentially just the CPU time.
Since we ran the I/O intensive command, most of the time it took for du to complete was spent waiting for the I/O operations to complete – as you can see from the example, the CPU time was minimal.
While the necessary file and directory attributes were being read from the disk, both the user time and the system time counters for the command were not clocking anything – your OS process scheduler was busy spending valuable CPU cycles to execute code for other processes.
- uptime – find out how long the system has been up
- top – show tasks and system status
- Basic Unix commands