{"id":1715,"date":"2021-07-11T18:35:23","date_gmt":"2021-07-11T13:05:23","guid":{"rendered":"https:\/\/jaipurhosting.com\/blog\/?p=1715"},"modified":"2021-07-11T18:35:27","modified_gmt":"2021-07-11T13:05:27","slug":"run-a-command-with-time-limit-timeout-in-linux","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/run-a-command-with-time-limit-timeout-in-linux\/","title":{"rendered":"Run a Command with Time Limit (Timeout) In Linux"},"content":{"rendered":"\n<p><strong>Description<\/strong><\/p>\n\n\n\n<p>Timeout is a command-line utility that runs a specified command and terminates it if it is still running after a given period of time.<\/p>\n\n\n\n<p>In other words, timeout allows you to run a command with a time limit.<\/p>\n\n\n\n<p>You can set a time limit for any command you want. If the time expires, the command stops executing.<\/p>\n\n\n\n<p>Here, we are going to learn two methods on how you can use a time limit in your commands.<\/p>\n\n\n\n<p><strong>Run Linux Commands Using the timeout Tool<\/strong><\/p>\n\n\n\n<p>Linux has a command-line utility called a timeout, which enables you to execute a command with a time limit.<\/p>\n\n\n\n<p>Its syntax is as follows.<\/p>\n\n\n\n<p><code>timeout [OPTION] DURATION COMMAND [ARG]\u2026<\/code><\/p>\n\n\n\n<p>To use the command, you specify a timeout value (in seconds) with the command you want to run.<\/p>\n\n\n\n<p>For instance, to timeout a ping command after 5 seconds, you can run the following command.<\/p>\n\n\n\n<p><code>timeout 5s ping jaipurhosting.com<\/code><\/p>\n\n\n\n<p>You do not have to specify the (s) after number 5. The command below is the same and will still work.<\/p>\n\n\n\n<p><code>timeout 5 ping jaipurhosting.com<\/code><\/p>\n\n\n\n<p>Other suffixes include:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>m representing minutes\nh representing hours\nd representing days<\/code><\/pre>\n\n\n\n<p>Sometimes commands may continue to run even after timeout sends the initial signal. In such instances, you can use the &#8211;kill-after option.<\/p>\n\n\n\n<p>Here\u2019s the syntax.<\/p>\n\n\n\n<p><code>-k, --kill-after=DURATION<\/code><\/p>\n\n\n\n<p>You need to specify a duration to let timeout know after how much time the kill signal is to be sent.<\/p>\n\n\n\n<p>For example, the command shown is going to be terminated after 8 seconds.<\/p>\n\n\n\n<p><code>timeout 8s tail -f \/var\/log\/syslog<\/code><\/p>\n\n\n\n<p><strong>Run Linux Commands Using Timelimit Program<\/strong><\/p>\n\n\n\n<p>The Timelimit program runs a given command then terminates the process after a specified time using a given signal. It initially passes a warning signal, and then after a timeout, it sends the kill signal.<\/p>\n\n\n\n<p>Unlike the timeout option, Timelimit has more options such as killsig, warnsig, killtime, and warntime.<\/p>\n\n\n\n<p>Timelimit can be found in the repositories of Debian-based systems and to install it, use the following command.<\/p>\n\n\n\n<p><code>sudo apt install timelimit<\/code><\/p>\n\n\n\n<p>For Arch-based systems, you can install it using AUR helper programs e.g., Pacaur Pacman, and Packer.<\/p>\n\n\n\n<p><code>Pacman -S timelimit<br>pacaur -S timelimit<br>packer -S timelimit<\/code><\/p>\n\n\n\n<p>After installation, run the following command and specify the time. <\/p>\n\n\n\n<p>Here, we can use 10 seconds.<\/p>\n\n\n\n<p><code>timelimit -t10 tail -f \/var\/log\/pacman.log<\/code><\/p>\n\n\n\n<p>Note that if you don\u2019t specify arguments, Timelimit uses the default values: warntime=3600 seconds, warnsig=15, killtime=120, and killsig=9.<\/p>\n\n\n\n<p><strong>Conclusion<\/strong><\/p>\n\n\n\n<p>Here, we have learned how to run commands with a time limit in Linux. In review, you can use the Timeout command or the Timelimit utility.<\/p>\n\n\n\n<p>The Timeout command is easy to use, but the Timelimit utility is a bit complicated but has more options.<\/p>\n\n\n\n<p>You can choose the most suitable option depending on your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description Timeout is a command-line utility that runs a specified command and terminates it if it is still running after a given period of time. In other words, timeout allows you to run a command with a time limit. You can set a time limit for any command you want. If the time expires, the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1715","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-categorized"],"_links":{"self":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1715"}],"version-history":[{"count":1,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1715\/revisions\/1716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1717"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}