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 April 3, 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 f | Fl -foreground 36.Op Fl k Ar time | Fl -kill-after Ar time 37.Op Fl p | Fl -preserve-status 38.Op Fl s Ar signal | Fl -signal Ar signal 39.Op Fl v | Fl -verbose 40.Ar duration 41.Ar command 42.Op Ar arg ... 43.Sh DESCRIPTION 44.Nm Timeout 45starts the 46.Ar command 47with its 48.Ar arg 49list. 50If the 51.Ar command 52is still running after 53.Ar duration , 54it is killed by sending the 55.Ar signal , 56or 57.Dv SIGTERM 58if the 59.Fl s 60option is unspecified. 61The special 62.Ar duration , 63zero, signifies no limit. 64Therefore, a signal is never sent if 65.Ar duration 66is 0. 67.Pp 68The signal dispositions inherited by the 69.Ar command 70are the same as the dispositions that 71.Nm 72inherited, except for the signal that will be sent upon timeout, 73which is reset to take the default action and should terminate 74the process. 75.Pp 76If 77.Nm 78receives the 79.Dv SIGALRM 80signal, it will behave as if the time limit has been reached 81and send the specified signal to 82.Ar command . 83For any other signals delivered to 84.Nm , 85it will propagate them to 86.Ar command , 87with the exception of 88.Dv SIGKILL 89and 90.Dv SIGSTOP . 91If you want to prevent the 92.Ar command 93from being timed out, send 94.Dv SIGKILL 95to 96.Nm . 97.Pp 98The options are as follows: 99.Bl -tag -width indent 100.It Fl f , Fl -foreground 101Only time out the 102.Ar command 103itself, but do not propagate signals to its descendants. 104.It Fl k Ar time , Fl -kill-after Ar time 105Send a 106.Dv SIGKILL 107signal if 108.Ar command 109is still running after 110.Ar time 111since the first signal was sent. 112.It Fl p , Fl -preserve-status 113Always exit with the same status as 114.Ar command , 115even if the timeout was reached. 116.It Fl s Ar signal , Fl -signal Ar signal 117Specify the signal to send on timeout. 118By default, 119.Dv SIGTERM 120is sent. 121.It Fl v , Fl -verbose 122Show information to 123.Xr stderr 4 124about timeouts, signals to be sent, and the 125.Ar command 126exits. 127.El 128.Ss Duration Format 129The 130.Ar duration 131and 132.Ar time 133are non-negative integer or real (decimal) numbers, with an optional 134suffix specifying the unit. 135Values without an explicit unit are interpreted as seconds. 136.Pp 137Supported unit suffixes are: 138.Bl -tag -offset indent -width indent -compact 139.It Cm s 140seconds 141.It Cm m 142minutes 143.It Cm h 144hours 145.It Cm d 146days 147.El 148.Sh EXIT STATUS 149If the timeout was not reached, the exit status of 150.Ar command 151is returned. 152.Pp 153If the timeout was reached and 154.Fl -preserve-status 155is set, the exit status of 156.Ar command 157is returned. 158If 159.Fl -preserve-status 160is not set, an exit status of 124 is returned. 161.Pp 162If an invalid parameter is passed to 163.Fl s 164or 165.Fl k , 166the exit status returned is 125. 167.Pp 168If 169.Ar command 170is an otherwise invalid program, the exit status returned is 126. 171.Pp 172If 173.Ar command 174refers to a non-existing program, the exit status returned is 127. 175.Pp 176If 177.Ar command 178exits after receiving a signal, the exit status returned is the signal number 179plus 128. 180.Sh EXAMPLES 181Run 182.Xr sleep 1 183with a time limit of 4 seconds. 184Since the command completes in 2 seconds, the exit status is 0: 185.Bd -literal -offset indent 186$ timeout 4 sleep 2 187$ echo $? 1880 189.Ed 190.Pp 191Run 192.Xr sleep 1 193for 4 seconds and terminate process after 2 seconds. 194The exit status is 124 since 195.Fl -preserve-status 196is not used: 197.Bd -literal -offset indent 198$ timeout 2 sleep 4 199$ echo $? 200124 201.Ed 202.Pp 203Same as above but preserving status. 204The exit status is 128 + signal number (15 for 205.Dv SIGTERM ) : 206.Bd -literal -offset indent 207$ timeout --preserve-status 2 sleep 4 208$ echo $? 209143 210.Ed 211.Pp 212Same as above but sending 213.Dv SIGALRM 214(signal number 14) instead of 215.Dv SIGTERM : 216.Bd -literal -offset indent 217$ timeout --preserve-status -s SIGALRM 2 sleep 4 218$ echo $? 219142 220.Ed 221.Pp 222Try to 223.Xr fetch 1 224the PDF version of the 225.Fx 226Handbook. 227Send a 228.Dv SIGTERM 229signal after 1 minute and send a 230.Dv SIGKILL 231signal 5 seconds later if the process refuses to stop: 232.Bd -literal -offset indent 233$ timeout -k 5s 1m fetch \\ 234> https://download.freebsd.org/ftp/doc/en/books/handbook/book.pdf 235.Ed 236.Sh SEE ALSO 237.Xr kill 1 , 238.Xr nohup 1 , 239.Xr signal 3 , 240.Xr daemon 8 241.Sh STANDARDS 242The 243.Nm 244utility is expected to conform to the 245.St -p1003.1-2024 246specification. 247.Sh HISTORY 248The 249.Nm 250command first appeared in 251.Fx 10.3 . 252.Pp 253The 254.Fx 255work is compatible with GNU 256.Nm 257by 258.An Padraig Brady , 259from GNU Coreutils 8.21. 260The 261.Nm 262utility first appeared in GNU Coreutils 7.0. 263.Sh AUTHORS 264.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org 265and 266.An Vsevolod Stakhov Aq Mt vsevolod@FreeBSD.org 267