{"id":8089,"date":"2018-12-20T18:48:36","date_gmt":"2018-12-20T15:48:36","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-time-command\/"},"modified":"2018-12-20T18:48:36","modified_gmt":"2018-12-20T15:48:36","slug":"%d8%a2%d9%85%d9%88%d8%b2%d8%b4-%d8%af%d8%b3%d8%aa%d9%88%d8%b1%d8%a7%d8%aa-%d8%b2%d9%85%d8%a7%d9%86-%d8%af%d8%b1-%d9%84%db%8c%d9%86%d9%88%da%a9%d8%b3","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/%d8%a2%d9%85%d9%88%d8%b2%d8%b4-%d8%af%d8%b3%d8%aa%d9%88%d8%b1%d8%a7%d8%aa-%d8%b2%d9%85%d8%a7%d9%86-%d8%af%d8%b1-%d9%84%db%8c%d9%86%d9%88%da%a9%d8%b3\/","title":{"rendered":"\u0622\u0645\u0648\u0632\u0634 \u062f\u0633\u062a\u0648\u0631\u0627\u062a \u0632\u0645\u0627\u0646 \u062f\u0631 \u0644\u06cc\u0646\u0648\u06a9\u0633"},"content":{"rendered":"<div dir=\"ltr\"><img decoding=\"async\" class=\"ff-og-image-inserted alignleft\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/12\/linux-time-command-tutorial-for-beginners-with-examples.jpg\" alt=\"\" title=\"\"><\/div>\n<p dir=\"ltr\" style=\"text-align: left;\">Sometimes, when you&#8217;re executing a program, you might want to know its system resource usage. Like how much time the process spent in kernel mode and user mode, and other info.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Thankfully, there exists a tool &#8211; dubbed <strong>time<\/strong> &#8211; that&#8217;s specifically built for this purpose. In this article, we will discuss the basics of the &#8216;time&#8217; command using some easy to understand examples.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">But before we do that, it&#8217;s worth mentioning that all examples in this tutorial have been tested on an Ubuntu 18.04 LTS machine.<\/p>\n<h2 id=\"linux-time-command\" dir=\"ltr\" style=\"text-align: left;\">Linux time command<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">The time command in Linux allows you to run programs and summarize their system resource usage. Following is its syntax:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">time [OPTIONS] COMMAND [ARGS]<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Here&#8217;s how the tool&#8217;s man page describes it:<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\">time run the program COMMAND with any given arguments ARG....\u00a0 When COMMAND finishes, time displays\ninformation about resources used by COMMAND (on the standard error output, by default).\u00a0<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">If COMMAND exits with non-zero status, time displays a warning message and the exit status.<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\"><\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">time determines which information to display about the resources used by the COMMAND from the<br \/>\nstring FORMAT. If no format is specified on the command line, but the TIME environment variable<br \/>\nis set, its value is used as the format. Otherwise, a default format built into time is used.<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\"><\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Options to time must appear on the command line before COMMAND.\u00a0 Anything on the command line after<br \/>\nCOMMAND is passed as arguments to COMMAND.<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\"><\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">Following are some Q&amp;A-styled examples that should give you a better idea on how the time command works.<\/p>\n<h2 id=\"q-how-to-use-time-command\" dir=\"ltr\" style=\"text-align: left;\">Q1. How to use time command?<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">The basic usage is simple &#8211; just execute &#8216;time&#8217; with the command\/program you want to run as input.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">For example, I used the time command in the following way:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">time ping howtoforge.com<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">And here&#8217;s the output:<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\">PING howtoforge.com (104.24.0.68) 56(84) bytes of data.\n64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=1 ttl=59 time=93.8 ms\n64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=2 ttl=59 time=91.5 ms\n64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=3 ttl=59 time=93.1 ms\n64 bytes from 104.24.0.68 (104.24.0.68): icmp_seq=4 ttl=59 time=102 ms\n^C\n--- howtoforge.com ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3003ms\nrtt min\/avg\/max\/mdev = 91.510\/95.249\/102.490\/4.267 ms<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>real\u00a0\u00a0 \u00a00m3.472s<\/strong><br \/>\n<strong>user\u00a0\u00a0 \u00a00m0.000s<\/strong><br \/>\n<strong>sys\u00a0\u00a0 \u00a00m0.004s<\/strong><\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\"><\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">The last three lines in the output are added by the time command. While &#8216;real&#8217; signifies the wall clock time the &#8216;ping&#8217; command took from execution till termination, &#8216;user&#8217; and &#8216;sys&#8217; are the time taken by &#8216;ping&#8217; the user space and kernel space, respectively. Detailed about these three times can be accessed <a href=\"https:\/\/stackoverflow.com\/a\/556411\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<h2 id=\"q-how-to-make-time-write-its-output-to-a-file\" dir=\"ltr\" style=\"text-align: left;\">Q2. How to make &#8216;time&#8217; write its output to a file?<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">If you want the time command to write its output to a file instead of the terminal, use the -o command line option, which expects a file name\/path as input.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">For example:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">\/usr\/bin\/time -o \/home\/himanshu\/time-output.txt ping howtoforge.com<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">This command will display ping&#8217;s output on stdout, while the &#8216;time&#8217; command output will be written to the text file.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><em><strong>NOTE<\/strong>: We used \/usr\/bin\/time instead of &#8216;time&#8217; because the shell built-in time command doesn&#8217;t offer the -o option.<\/em><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">By default, everytime you run this command, the output file will be overwritten. However, if you want, you can make sure new output is appended by using the -a command line option.<\/p>\n<h2 id=\"q-how-to-make-time-produce-detailed-output\" dir=\"ltr\" style=\"text-align: left;\">Q3. How to make time produce detailed output?<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">This can be done using the -v command line option. For example, when I used this option while running &#8216;time&#8217; with a &#8216;ping&#8217; command, following details were produced in the &#8216;time&#8217; command output:<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\">Command being timed: \"ping howtoforge.com\"\n\u00a0\u00a0 \u00a0User time (seconds): 0.00\n\u00a0\u00a0 \u00a0System time (seconds): 0.00\n\u00a0\u00a0 \u00a0Percent of CPU this job got: 0%\n\u00a0\u00a0 \u00a0Elapsed (wall clock) time (h:mm:ss or m:ss): 0:11.77\n\u00a0\u00a0 \u00a0Average shared text size (kbytes): 0\n\u00a0\u00a0 \u00a0Average unshared data size (kbytes): 0\n\u00a0\u00a0 \u00a0Average stack size (kbytes): 0\n\u00a0\u00a0 \u00a0Average total size (kbytes): 0\n\u00a0\u00a0 \u00a0Maximum resident set size (kbytes): 3064\n\u00a0\u00a0 \u00a0Average resident set size (kbytes): 0\n\u00a0\u00a0 \u00a0Major (requiring I\/O) page faults: 0\n\u00a0\u00a0 \u00a0Minor (reclaiming a frame) page faults: 158\n\u00a0\u00a0 \u00a0Voluntary context switches: 14\n\u00a0\u00a0 \u00a0Involuntary context switches: 0\n\u00a0\u00a0 \u00a0Swaps: 0\n\u00a0\u00a0 \u00a0File system inputs: 0\n\u00a0\u00a0 \u00a0File system outputs: 0\n\u00a0\u00a0 \u00a0Socket messages sent: 0\n\u00a0\u00a0 \u00a0Socket messages received: 0\n\u00a0\u00a0 \u00a0Signals delivered: 0\n\u00a0\u00a0 \u00a0Page size (bytes): 4096\n\u00a0\u00a0 \u00a0Exit status: 0<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">So you can see the time command produces a lot of other details in this mode.<\/p>\n<h2 id=\"q-how-to-customize-time-command-output\" dir=\"ltr\" style=\"text-align: left;\">Q4. How to customize time command output?<\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">The time command also offers a &#8216;format&#8217; command line option that lets you customize the output of this tool. It provides a set of resource specifiers that you can use to fetch any type of information supported by the &#8216;time&#8217; command (see previous section).<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">For example, the time command in the following execution:<\/p>\n<pre class=\"command\" dir=\"ltr\"><code spellcheck=\"false\">\/usr\/bin\/time -f \"\\t%C [Command details],\\t%K [Total memory usage],\\t%k [Number of signals process received]\" ping howtoforge.com<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">produced this output:<\/p>\n<pre dir=\"ltr\"><code spellcheck=\"false\">ping howtoforge.com [Command details],\u00a0\u00a0 \u00a00 [Total memory usage],\u00a0\u00a0 \u00a00 [Number of signals process received]<\/code><\/pre>\n<p dir=\"ltr\" style=\"text-align: left;\">The time command man page contains details related to the format command line option.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, when you&#8217;re executing a program, you might want to know its system resource usage. Like how much time the process spent in kernel mode and user mode, and other info. Thankfully, there exists a tool &#8211; dubbed time &#8211; that&#8217;s specifically built for this purpose. In this article, we will discuss the basics of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-8089","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/8089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=8089"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/8089\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=8089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=8089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=8089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}