1.\" SPDX-License-Identifier: BSD-2-Clause 2.\" 3.\" Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org> 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.Dd January 4, 2025 28.Dt TIMEOUT 1 29.Os 30.Sh NAME 31.Nm timeout 32.Nd run a command with a time limit 33.Sh SYNOPSIS 34.Nm 35.Op Fl k Ar time | Fl -kill-after Ar time 36.Op Fl s Ar sig | Fl -signal Ar sig 37.Op Fl v | Fl -verbose 38.Op Fl -foreground 39.Op Fl -preserve-status 40.Ar duration 41.Ar command 42.Op Ar args ... 43.Sh DESCRIPTION 44.Nm 45starts the 46.Ar command 47with its 48.Ar args . 49If the 50.Ar command 51is still running after 52.Ar duration , 53it is killed. 54By default, 55.Dv SIGTERM 56is sent. 57The special 58.Ar duration , 59zero, signifies no limit. 60Therefore a signal is never sent if 61.Ar duration 62is 0. 63.Pp 64The options are as follows: 65.Bl -tag -width indent 66.It Fl k Ar time , Fl -kill-after Ar time 67Send a 68.Dv SIGKILL 69signal if 70.Ar command 71is still running after 72.Ar time 73after the first signal was sent. 74.It Fl s Ar sig , Fl -signal Ar sig 75Specify the signal to send on timeout. 76By default, 77.Dv SIGTERM 78is sent. 79.It Fl v , Fl -verbose 80Show information to stderr about any signal sent on timeout. 81.It Fl -foreground 82Do not propagate timeout to the children of 83.Ar command . 84.It Fl -preserve-status 85Exit with the same status as 86.Ar command , 87even if it times out and is killed. 88.El 89.Sh DURATION FORMAT 90The 91.Ar duration 92and 93.Ar time 94are non-negative integer or real (decimal) numbers, with an optional 95unit-specifying suffix. 96Values without an explicit unit are interpreted as seconds. 97.Pp 98Supported unit symbols are: 99.Bl -tag -offset indent -width indent -compact 100.It Cm s 101seconds 102.It Cm m 103minutes 104.It Cm h 105hours 106.It Cm d 107days 108.El 109.Sh EXIT STATUS 110If the timeout was not reached, the exit status of 111.Ar command 112is returned. 113.Pp 114If the timeout was reached and 115.Fl -preserve-status 116is set, the exit status of 117.Ar command 118is returned. 119If 120.Fl -preserve-status 121is not set, an exit status of 124 is returned. 122.Pp 123If an invalid parameter is passed to 124.Fl s 125or 126.Fl k , 127the exit status returned is 125. 128.Pp 129If 130.Ar command 131is an otherwise invalid program, the exit status returned is 126. 132.Pp 133If 134.Ar command 135refers to a non-existing program, the exit status returned is 127. 136.Pp 137If 138.Ar command 139exits after receiving a signal, the exit status returned is the signal number 140plus 128. 141.Sh EXAMPLES 142Run 143.Xr sleep 1 144with a time limit of 4 seconds. 145Since the command completes in 2 seconds, the exit status is 0: 146.Bd -literal -offset indent 147$ timeout 4 sleep 2 148$ echo $? 1490 150.Ed 151.Pp 152Run 153.Xr sleep 1 154for 4 seconds and terminate process after 2 seconds. 155124 is returned since no 156.Fl -preserve-status 157is used: 158.Bd -literal -offset indent 159$ timeout 2 sleep 4 160$ echo $? 161124 162.Ed 163.Pp 164Same as above but preserving status. 165Exit status is 128 + signal number (15 for 166.Va SIGTERM ) : 167.Bd -literal -offset indent 168$ timeout --preserve-status 2 sleep 4 169$ echo $? 170143 171.Ed 172.Pp 173Same as above but sending 174.Va SIGALRM 175(signal number 14) instead of 176.Va SIGTERM : 177.Bd -literal -offset indent 178$ timeout --preserve-status -s SIGALRM 2 sleep 4 179$ echo $? 180142 181.Ed 182.Pp 183Try to 184.Xr fetch 1 185the PDF version of the 186.Fx 187Handbook. 188Send a 189.Va SIGTERM 190signal after 1 minute and send a 191.Va SIGKILL 192signal 5 seconds later if the process refuses to stop: 193.Bd -literal -offset indent 194$ timeout -k 5s 1m fetch \\ 195> https://download.freebsd.org/ftp/doc/en/books/handbook/book.pdf 196.Ed 197.Sh SEE ALSO 198.Xr kill 1 , 199.Xr nohup 1 , 200.Xr signal 3 , 201.Xr daemon 8 202.Sh STANDARDS 203The 204.Nm 205utility is compliant with the 206.St -p1003.1-2024 207specification. 208.Sh HISTORY 209The 210.Nm 211command first appeared in 212.Fx 10.3 . 213.Pp 214The 215.Fx 216work is compatible with GNU 217.Nm 218by 219.An Padraig Brady , 220from GNU Coreutils 8.21. 221The 222.Nm 223utility first appeared in GNU Coreutils 7.0. 224.Sh AUTHORS 225.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org 226and 227.An Vsevolod Stakhov Aq Mt vsevolod@FreeBSD.org 228