{"id":1685,"date":"2019-12-13T11:07:11","date_gmt":"2019-12-13T05:37:11","guid":{"rendered":"http:\/\/jaipurhosting.com\/blog\/?p=1685"},"modified":"2019-12-13T11:31:00","modified_gmt":"2019-12-13T06:01:00","slug":"how-to-run-a-command-with-time-limit-timeout-in-linux","status":"publish","type":"post","link":"https:\/\/www.jaipurhosting.com\/blog\/how-to-run-a-command-with-time-limit-timeout-in-linux\/","title":{"rendered":"How to Run a Command with Time Limit (Timeout) In Linux"},"content":{"rendered":"\n<p><strong>Description : <\/strong><\/p>\n\n\n\n<p>The goal of Linux is to help you be as fast and efficient as possible. One property of a Linux command is the 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>In this short tutorial, you 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&nbsp;<strong>timeout<\/strong>, 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<pre class=\"wp-block-code\"><code>timeout [OPTION] DURATION COMMAND [ARG]...<\/code><\/pre>\n\n\n\n<p> To use the command, you specify a&nbsp;<strong>timeout<\/strong>&nbsp;value (in seconds) with the command you want to run. <\/p>\n\n\n\n<p>For instance, to timeout a&nbsp;<a rel=\"noreferrer noopener nofollow\" href=\"https:\/\/www.tecmint.com\/linux-ping-command-examples\/\" target=\"_blank\">ping command<\/a>&nbsp;after&nbsp;<strong>5<\/strong>&nbsp;seconds, you can run the following command. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># timeout 5s ping google.com<\/code><\/pre>\n\n\n\n<p> You do not have to specify the&nbsp;<strong>(s)<\/strong>&nbsp;after number&nbsp;<strong>5<\/strong>. The command below is the same and will still work. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># timeout 5 ping google.com<\/code><\/pre>\n\n\n\n<p>Other suffixes include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m<\/code>&nbsp;representing minutes<\/li><li><code>h<\/code>&nbsp;representing hours<\/li><li><code>d<\/code>&nbsp;representing days<\/li><\/ul>\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&nbsp;<code>--kill-after<\/code>&nbsp;option.<\/p>\n\n\n\n<p>Here\u2019s the syntax.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-k, --kill-after=DURATION<\/code><\/pre>\n\n\n\n<p>You need to specify a duration to let&nbsp;<strong>timeout<\/strong>&nbsp;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&nbsp;<strong>8<\/strong>&nbsp;seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># timeout 8s tail -f \/var\/log\/syslog<\/code><\/pre>\n\n\n\n<p><strong>Run Linux Commands Using Timelimit Program<\/strong><\/p>\n\n\n\n<p>The&nbsp;<strong>Timelimit<\/strong>&nbsp;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&nbsp;<strong>timeout<\/strong>&nbsp;option,&nbsp;<strong>Timelimit<\/strong>&nbsp;has more options such as&nbsp;<strong>killsig<\/strong>,&nbsp;<strong>warnsig<\/strong>,&nbsp;<strong>killtime<\/strong>, and&nbsp;<strong>warntime<\/strong>.<\/p>\n\n\n\n<p><strong>Timelimit<\/strong>&nbsp;can be found in the repositories of&nbsp;<strong>Debian<\/strong>-based systems and to install it, use the following command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install timelimit<\/code><\/pre>\n\n\n\n<p> For&nbsp;<strong>Arch-based<\/strong>&nbsp;systems, you can install it using&nbsp;<strong>AUR<\/strong>&nbsp;helper programs e.g.,&nbsp;<strong>Pacaur<\/strong>&nbsp;<strong>Pacman<\/strong>, and&nbsp;<strong>Packer<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Pacman -S timelimit\n# pacaur -S timelimit\n# packer -S timelimit<\/code><\/pre>\n\n\n\n<p> After installation, run the following command and specify the time. In this example, you can use&nbsp;<strong>10<\/strong>&nbsp;seconds. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ timelimit -t10 tail -f \/var\/log\/pacman.log<\/code><\/pre>\n\n\n\n<p> Note that if you don\u2019t specify arguments,&nbsp;<strong>Timelimit<\/strong>&nbsp;uses the default values:&nbsp;<strong>warntime=3600<\/strong>&nbsp;seconds,&nbsp;<strong>warnsig=15<\/strong>,&nbsp;<strong>killtime=120<\/strong>, and&nbsp;<strong>killsig=9<\/strong>. <\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Conclusion<\/h5>\n\n\n\n<p>The&nbsp;<strong>Timeout<\/strong>&nbsp;command is easy to use, but the&nbsp;<strong>Timelimit<\/strong>&nbsp;utility is a bit complicated but has more options. You can choose the most suitable option depending on your needs.<br><\/p>\n\n\n\n<p> We hope you\u2019ve found this useful! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Description : The goal of Linux is to help you be as fast and efficient as possible. One property of a Linux command is the time limit. You can set a time limit for any command you want. If the time expires, the command stops executing. In this short tutorial, you are going to learn [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1685","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\/1685","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=1685"}],"version-history":[{"count":2,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1685\/revisions"}],"predecessor-version":[{"id":1689,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1685\/revisions\/1689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media\/1688"}],"wp:attachment":[{"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jaipurhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}