Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following script shows how to do this using background tasks. The first section kills a 60-second process after the 10-second limit. The second attempts to kill a process that's already exited. Keep in mind that, if you set your timeout really high, the process IDs may roll over and you'll kill the wrong process but this is more of a theoretical issue - the timeout would have to be <em>very</em> large and you would have to be starting a <em>lot</em> of processes.</p> <pre><code>#!/usr/bin/bash sleep 60 &amp; pid=$! sleep 10 kill -9 $pid sleep 3 &amp; pid=$! sleep 10 kill -9 $pid </code></pre> <p>Here's the output on my Cygwin box:</p> <pre><code>$ ./limit10 ./limit10: line 9: 4492 Killed sleep 60 ./limit10: line 11: kill: (4560) - No such process </code></pre> <p>If you want to only wait until the process has finished, you need to enter a loop and check. This is slightly less accurate since <code>sleep 1</code> and the other commands will actually take more than one second (but not much more). Use this script to replace the second section above (the "<code>echo $proc</code>" and "<code>date</code>" commands are for debugging, I wouldn't expect to have them in the final solution).</p> <pre><code>#!/usr/bin/bash date sleep 3 &amp; pid=$! ((lim = 10)) while [[ $lim -gt 0 ]] ; do sleep 1 proc=$(ps -ef | awk -v pid=$pid '$2==pid{print}{}') echo $proc ((lim = lim - 1)) if [[ -z "$proc" ]] ; then ((lim = -9)) fi done date if [[ $lim -gt -9 ]] ; then kill -9 $pid fi date </code></pre> <p>It basically loops, checking if the process is still running every second. If not, it exits the loop with a special value to not try and kill the child. Otherwise it times out and does kill the child.</p> <p>Here's the output for a <code>sleep 3</code>:</p> <pre><code>Mon Feb 9 11:10:37 WADT 2009 pax 4268 2476 con 11:10:37 /usr/bin/sleep pax 4268 2476 con 11:10:37 /usr/bin/sleep Mon Feb 9 11:10:41 WADT 2009 Mon Feb 9 11:10:41 WADT 2009 </code></pre> <p>and a <code>sleep 60</code>:</p> <pre><code>Mon Feb 9 11:11:51 WADT 2009 pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep pax 4176 2600 con 11:11:51 /usr/bin/sleep Mon Feb 9 11:12:03 WADT 2009 Mon Feb 9 11:12:03 WADT 2009 ./limit10: line 20: 4176 Killed sleep 60 </code></pre>
    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. 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