{"id":52,"date":"2024-01-03T05:37:45","date_gmt":"2024-01-03T05:37:45","guid":{"rendered":"https:\/\/drupalwebhosting.in\/tutorials\/?p=52"},"modified":"2024-01-03T08:47:55","modified_gmt":"2024-01-03T08:47:55","slug":"run-ssh-command-with-time-limit","status":"publish","type":"post","link":"https:\/\/drupalwebhosting.in\/tutorials\/run-ssh-command-with-time-limit\/","title":{"rendered":"Run a Command with Time Limit"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p>Linux comes with a&nbsp;bounty of commands, each command unique and used in specific cases. 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.<\/p>\n\n\n\n<p>In Linux, you can use the <code>timeout<\/code> command to execute a command with a specified time limit. This command allows you to set a maximum duration for the execution of another command. The syntax for using <code>timeout<\/code> is:<\/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<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"678\" height=\"381\" src=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/Linux-5.15-released-1.webp\" alt=\"\" class=\"wp-image-63\" style=\"width:840px;height:auto\" srcset=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/Linux-5.15-released-1.webp 678w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/Linux-5.15-released-1-300x169.webp 300w\" sizes=\"auto, (max-width: 678px) 100vw, 678px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#run-linux-commands-using-the-timeout-tool\">Run Linux Commands Using the timeout Tool<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"run-linux-commands-using-the-timeout-tool\">Run Linux Commands Using the timeout Tool<\/h3>\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 &#91;OPTION] DURATION COMMAND &#91;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. For instance, to timeout a&nbsp;ping command&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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"139\" src=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1-1024x139.png\" alt=\"time limit\" class=\"wp-image-68\" srcset=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1-1024x139.png 1024w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1-300x41.png 300w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1-768x105.png 768w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1-1536x209.png 1536w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/1.png 1550w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"136\" src=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2-1024x136.png\" alt=\"\" class=\"wp-image-71\" srcset=\"https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2-1024x136.png 1024w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2-300x40.png 300w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2-768x102.png 768w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2-1536x203.png 1536w, https:\/\/drupalwebhosting.in\/tutorials\/wp-content\/uploads\/2023\/12\/2.png 1624w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>To run the <code>sleep<\/code> command for a maximum of 5 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>timeout 5s sleep 10<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This command will run <code>sleep 10<\/code>, but it will be terminated after 5 seconds.<\/p>\n\n\n\n<p>The <code>timeout<\/code> command also offers various options that can be used to customize its behavior<\/p>\n\n\n\n<p><code>-s<\/code>, <code>--signal=SIGNAL<\/code>: Specifies the signal to send on timeout (default is SIGTERM).<\/p>\n\n\n\n<p><code>-k<\/code>, <code>--kill-after=DURATION<\/code>: Sends a specified signal if the command runs for longer than the specified duration.<\/p>\n\n\n\n<p><code>-v<\/code>, <code>--verbose<\/code>: Provides verbose output.<\/p>\n\n\n\n<p><code>--preserve-status<\/code>: Exits with the command&#8217;s exit status, even if terminated due to timeout.<\/p>\n\n\n\n<p>For example, to send a <code>SIGKILL<\/code> signal to the command if it exceeds the timeout duration of 10 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>timeout --signal=SIGKILL 10s google.com<\/code><\/pre>\n\n\n\n<p>Other suffixes include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>m<\/code>&nbsp;representing minutes<\/li>\n\n\n\n<li><code>h<\/code>&nbsp;representing hours<\/li>\n\n\n\n<li><code>d<\/code>&nbsp;representing days<\/li>\n<\/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>If you want to check the <a href=\"https:\/\/drupalwebhosting.in\/tutorials\/ssh-command-to-view-disk-usage\/\" data-type=\"link\" data-id=\"https:\/\/drupalwebhosting.in\/tutorials\/ssh-command-to-view-disk-usage\/\">disk usage<\/a> within a specified time limit using the <code>timeout<\/code> command, you can combine the <code>du<\/code> (disk usage) command with the <code>timeout<\/code> command. <\/p>\n\n\n\n<p>For instance, if you want to limit the execution of <code>du<\/code> to 5 seconds:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>timeout 5s du -sh \/path\/to\/directory<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"run-linux-commands-using-time-limit-program\">Run Linux Commands Using Time limit Program<\/h2>\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\npacaur -S timelimit\npacker -S timelimit<\/code><\/pre>\n\n\n\n<p>Other Linux distributions, you can&nbsp;download timelimit source&nbsp;and manually install it.<\/p>\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<p>In this guide, you have learned how to run commands with a time limit in Linux. In review, you can use the&nbsp;<strong>Timeout<\/strong>&nbsp;command or the&nbsp;<strong>Timelimit<\/strong>&nbsp;utility.<\/p>\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.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We hope you&#8217;ve found this useful!!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux comes with a&nbsp;bounty of commands, each command unique and used in specific cases. 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 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":62,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[6,7],"class_list":["post-52","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","tag-time-limit","tag-timeout"],"_links":{"self":[{"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/posts\/52","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":20,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/posts\/52\/revisions\/91"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/media\/62"}],"wp:attachment":[{"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/drupalwebhosting.in\/tutorials\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}