Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've arrived rather late to this party, but I don't see my favorite trick listed in the answers.</p> <p>Under *NIX, an <code>alarm(2)</code> is inherited across an <code>execve(2)</code> and SIGALRM is fatal by default. So, you can often simply:</p> <pre><code>$ doalarm () { perl -e 'alarm shift; exec @ARGV' "$@"; } # define a helper function $ doalarm 300 ./foo.sh args </code></pre> <p>or install a <a href="http://www.pilcrow.madison.wi.us/" rel="noreferrer">trivial C wrapper</a> to do that for you.</p> <p><strong>Advantages</strong> Only one PID is involved, and the mechanism is simple. You won't kill the wrong process if, for example, <code>./foo.sh</code> exited "too quickly" and its PID was re-used. You don't need several shell subprocesses working in concert, which can be done correctly but is rather race-prone.</p> <p><strong>Disadvantages</strong> The time-constrained process cannot manipulate its alarm clock (e.g., <code>alarm(2)</code>, <code>ualarm(2)</code>, <code>setitimer(2)</code>), since this would likely clear the inherited alarm. Obviously, neither can it block or ignore SIGALRM, though the same can be said of SIGINT, SIGTERM, etc. for some other approaches.</p> <p>Some (very old, I think) systems implement <code>sleep(2)</code> in terms of <code>alarm(2)</code>, and, even today, some programmers use <code>alarm(2)</code> as a crude internal timeout mechanism for I/O and other operations. In my experience, however, this technique is applicable to the vast majority of processes you want to time limit.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload