14d846d26SWarner Losh.\" SPDX-License-Identifier: BSD-2-Clause 2e7ab1336SMateusz Piotrowski.\" 3e7ab1336SMateusz Piotrowski.\" Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org> 4e7ab1336SMateusz Piotrowski.\" All rights reserved. 5e7ab1336SMateusz Piotrowski.\" 6e7ab1336SMateusz Piotrowski.\" Redistribution and use in source and binary forms, with or without 7e7ab1336SMateusz Piotrowski.\" modification, are permitted provided that the following conditions 8e7ab1336SMateusz Piotrowski.\" are met: 9e7ab1336SMateusz Piotrowski.\" 1. Redistributions of source code must retain the above copyright 10e7ab1336SMateusz Piotrowski.\" notice, this list of conditions and the following disclaimer. 11e7ab1336SMateusz Piotrowski.\" 2. Redistributions in binary form must reproduce the above copyright 12e7ab1336SMateusz Piotrowski.\" notice, this list of conditions and the following disclaimer in the 13e7ab1336SMateusz Piotrowski.\" documentation and/or other materials provided with the distribution. 14e7ab1336SMateusz Piotrowski.\" 15e7ab1336SMateusz Piotrowski.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16e7ab1336SMateusz Piotrowski.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17e7ab1336SMateusz Piotrowski.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18e7ab1336SMateusz Piotrowski.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19e7ab1336SMateusz Piotrowski.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20e7ab1336SMateusz Piotrowski.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21e7ab1336SMateusz Piotrowski.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22e7ab1336SMateusz Piotrowski.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23e7ab1336SMateusz Piotrowski.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24e7ab1336SMateusz Piotrowski.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25e7ab1336SMateusz Piotrowski.\" SUCH DAMAGE. 26e7ab1336SMateusz Piotrowski.\" 27*d633a7d1SGordon Bergling.Dd January 4, 2025 28e7ab1336SMateusz Piotrowski.Dt TIMEOUT 1 29e7ab1336SMateusz Piotrowski.Os 30e7ab1336SMateusz Piotrowski.Sh NAME 31e7ab1336SMateusz Piotrowski.Nm timeout 32e7ab1336SMateusz Piotrowski.Nd run a command with a time limit 33e7ab1336SMateusz Piotrowski.Sh SYNOPSIS 34e7ab1336SMateusz Piotrowski.Nm 3506690044SGordon Bergling.Op Fl k Ar time | Fl -kill-after Ar time 3606690044SGordon Bergling.Op Fl s Ar sig | Fl -signal Ar sig 37*d633a7d1SGordon Bergling.Op Fl v | Fl -verbose 38e7ab1336SMateusz Piotrowski.Op Fl -foreground 3906690044SGordon Bergling.Op Fl -preserve-status 40e7ab1336SMateusz Piotrowski.Ar duration 41e7ab1336SMateusz Piotrowski.Ar command 42e7ab1336SMateusz Piotrowski.Op Ar args ... 43e7ab1336SMateusz Piotrowski.Sh DESCRIPTION 44e7ab1336SMateusz Piotrowski.Nm 45e7ab1336SMateusz Piotrowskistarts the 46e7ab1336SMateusz Piotrowski.Ar command 47e7ab1336SMateusz Piotrowskiwith its 48e7ab1336SMateusz Piotrowski.Ar args . 49e7ab1336SMateusz PiotrowskiIf the 50e7ab1336SMateusz Piotrowski.Ar command 51e7ab1336SMateusz Piotrowskiis still running after 52e7ab1336SMateusz Piotrowski.Ar duration , 53e7ab1336SMateusz Piotrowskiit is killed. 54e7ab1336SMateusz PiotrowskiBy default, 55e7ab1336SMateusz Piotrowski.Dv SIGTERM 56e7ab1336SMateusz Piotrowskiis sent. 57e7ab1336SMateusz PiotrowskiThe special 58e7ab1336SMateusz Piotrowski.Ar duration , 59e7ab1336SMateusz Piotrowskizero, signifies no limit. 60e7ab1336SMateusz PiotrowskiTherefore a signal is never sent if 61e7ab1336SMateusz Piotrowski.Ar duration 62e7ab1336SMateusz Piotrowskiis 0. 63e7ab1336SMateusz Piotrowski.Pp 64e7ab1336SMateusz PiotrowskiThe options are as follows: 65e7ab1336SMateusz Piotrowski.Bl -tag -width indent 66e7ab1336SMateusz Piotrowski.It Fl k Ar time , Fl -kill-after Ar time 67e7ab1336SMateusz PiotrowskiSend a 68e7ab1336SMateusz Piotrowski.Dv SIGKILL 69e7ab1336SMateusz Piotrowskisignal if 70e7ab1336SMateusz Piotrowski.Ar command 71e7ab1336SMateusz Piotrowskiis still running after 72e7ab1336SMateusz Piotrowski.Ar time 73e7ab1336SMateusz Piotrowskiafter the first signal was sent. 7406690044SGordon Bergling.It Fl s Ar sig , Fl -signal Ar sig 7506690044SGordon BerglingSpecify the signal to send on timeout. 7606690044SGordon BerglingBy default, 7706690044SGordon Bergling.Dv SIGTERM 7806690044SGordon Berglingis sent. 79*d633a7d1SGordon Bergling.It Fl v , Fl -verbose 80*d633a7d1SGordon BerglingShow information to stderr about any signal sent on timeout. 8106690044SGordon Bergling.It Fl -foreground 8206690044SGordon BerglingDo not propagate timeout to the children of 8306690044SGordon Bergling.Ar command . 8406690044SGordon Bergling.It Fl -preserve-status 8506690044SGordon BerglingExit with the same status as 8606690044SGordon Bergling.Ar command , 8706690044SGordon Berglingeven if it times out and is killed. 88e7ab1336SMateusz Piotrowski.El 89e7ab1336SMateusz Piotrowski.Sh DURATION FORMAT 9006690044SGordon BerglingThe 91e7ab1336SMateusz Piotrowski.Ar duration 92e7ab1336SMateusz Piotrowskiand 93e7ab1336SMateusz Piotrowski.Ar time 94e7ab1336SMateusz Piotrowskiare non-negative integer or real (decimal) numbers, with an optional 95e7ab1336SMateusz Piotrowskiunit-specifying suffix. 96e7ab1336SMateusz PiotrowskiValues without an explicit unit are interpreted as seconds. 97e7ab1336SMateusz Piotrowski.Pp 98e7ab1336SMateusz PiotrowskiSupported unit symbols are: 9906690044SGordon Bergling.Bl -tag -offset indent -width indent -compact 100e7ab1336SMateusz Piotrowski.It Cm s 101e7ab1336SMateusz Piotrowskiseconds 102e7ab1336SMateusz Piotrowski.It Cm m 103e7ab1336SMateusz Piotrowskiminutes 104e7ab1336SMateusz Piotrowski.It Cm h 105e7ab1336SMateusz Piotrowskihours 106e7ab1336SMateusz Piotrowski.It Cm d 107e7ab1336SMateusz Piotrowskidays 108e7ab1336SMateusz Piotrowski.El 109e7ab1336SMateusz Piotrowski.Sh EXIT STATUS 110e7ab1336SMateusz PiotrowskiIf the timeout was not reached, the exit status of 111e7ab1336SMateusz Piotrowski.Ar command 112e7ab1336SMateusz Piotrowskiis returned. 113e7ab1336SMateusz Piotrowski.Pp 114e7ab1336SMateusz PiotrowskiIf the timeout was reached and 115e7ab1336SMateusz Piotrowski.Fl -preserve-status 116e7ab1336SMateusz Piotrowskiis set, the exit status of 117e7ab1336SMateusz Piotrowski.Ar command 118e7ab1336SMateusz Piotrowskiis returned. 119e7ab1336SMateusz PiotrowskiIf 120e7ab1336SMateusz Piotrowski.Fl -preserve-status 121e7ab1336SMateusz Piotrowskiis not set, an exit status of 124 is returned. 122e7ab1336SMateusz Piotrowski.Pp 12306690044SGordon BerglingIf an invalid parameter is passed to 12406690044SGordon Bergling.Fl s 12506690044SGordon Berglingor 12606690044SGordon Bergling.Fl k , 12706690044SGordon Berglingthe exit status returned is 125. 12806690044SGordon Bergling.Pp 129e7ab1336SMateusz PiotrowskiIf 130e7ab1336SMateusz Piotrowski.Ar command 13106690044SGordon Berglingis an otherwise invalid program, the exit status returned is 126. 132e7ab1336SMateusz Piotrowski.Pp 133e7ab1336SMateusz PiotrowskiIf 134e7ab1336SMateusz Piotrowski.Ar command 135e7ab1336SMateusz Piotrowskirefers to a non-existing program, the exit status returned is 127. 136e7ab1336SMateusz Piotrowski.Pp 137e7ab1336SMateusz PiotrowskiIf 138e7ab1336SMateusz Piotrowski.Ar command 13906690044SGordon Berglingexits after receiving a signal, the exit status returned is the signal number 14006690044SGordon Berglingplus 128. 141e7ab1336SMateusz Piotrowski.Sh EXAMPLES 142e7ab1336SMateusz PiotrowskiRun 143e7ab1336SMateusz Piotrowski.Xr sleep 1 144e7ab1336SMateusz Piotrowskiwith a time limit of 4 seconds. 145e7ab1336SMateusz PiotrowskiSince the command completes in 2 seconds, the exit status is 0: 146e7ab1336SMateusz Piotrowski.Bd -literal -offset indent 147e7ab1336SMateusz Piotrowski$ timeout 4 sleep 2 148e7ab1336SMateusz Piotrowski$ echo $? 149e7ab1336SMateusz Piotrowski0 150e7ab1336SMateusz Piotrowski.Ed 151e7ab1336SMateusz Piotrowski.Pp 152e7ab1336SMateusz PiotrowskiRun 153e7ab1336SMateusz Piotrowski.Xr sleep 1 154e7ab1336SMateusz Piotrowskifor 4 seconds and terminate process after 2 seconds. 155e7ab1336SMateusz Piotrowski124 is returned since no 156e7ab1336SMateusz Piotrowski.Fl -preserve-status 157e7ab1336SMateusz Piotrowskiis used: 158e7ab1336SMateusz Piotrowski.Bd -literal -offset indent 159e7ab1336SMateusz Piotrowski$ timeout 2 sleep 4 160e7ab1336SMateusz Piotrowski$ echo $? 161e7ab1336SMateusz Piotrowski124 162e7ab1336SMateusz Piotrowski.Ed 163e7ab1336SMateusz Piotrowski.Pp 164e7ab1336SMateusz PiotrowskiSame as above but preserving status. 165e7ab1336SMateusz PiotrowskiExit status is 128 + signal number (15 for 166e7ab1336SMateusz Piotrowski.Va SIGTERM ) : 167e7ab1336SMateusz Piotrowski.Bd -literal -offset indent 168e7ab1336SMateusz Piotrowski$ timeout --preserve-status 2 sleep 4 169e7ab1336SMateusz Piotrowski$ echo $? 170e7ab1336SMateusz Piotrowski143 171e7ab1336SMateusz Piotrowski.Ed 172e7ab1336SMateusz Piotrowski.Pp 173e7ab1336SMateusz PiotrowskiSame as above but sending 174e7ab1336SMateusz Piotrowski.Va SIGALRM 175e7ab1336SMateusz Piotrowski(signal number 14) instead of 176e7ab1336SMateusz Piotrowski.Va SIGTERM : 177e7ab1336SMateusz Piotrowski.Bd -literal -offset indent 178e7ab1336SMateusz Piotrowski$ timeout --preserve-status -s SIGALRM 2 sleep 4 179e7ab1336SMateusz Piotrowski$ echo $? 180e7ab1336SMateusz Piotrowski142 181e7ab1336SMateusz Piotrowski.Ed 182e7ab1336SMateusz Piotrowski.Pp 183e7ab1336SMateusz PiotrowskiTry to 184e7ab1336SMateusz Piotrowski.Xr fetch 1 185e7ab1336SMateusz Piotrowskithe PDF version of the 186e7ab1336SMateusz Piotrowski.Fx 187e7ab1336SMateusz PiotrowskiHandbook. 188e7ab1336SMateusz PiotrowskiSend a 189e7ab1336SMateusz Piotrowski.Va SIGTERM 190e7ab1336SMateusz Piotrowskisignal after 1 minute and send a 191e7ab1336SMateusz Piotrowski.Va SIGKILL 192e7ab1336SMateusz Piotrowskisignal 5 seconds later if the process refuses to stop: 193e7ab1336SMateusz Piotrowski.Bd -literal -offset indent 194e7ab1336SMateusz Piotrowski$ timeout -k 5s 1m fetch \\ 195e7ab1336SMateusz Piotrowski> https://download.freebsd.org/ftp/doc/en/books/handbook/book.pdf 196e7ab1336SMateusz Piotrowski.Ed 197e7ab1336SMateusz Piotrowski.Sh SEE ALSO 198e7ab1336SMateusz Piotrowski.Xr kill 1 , 19970bc3f43STom Hukins.Xr nohup 1 , 20070bc3f43STom Hukins.Xr signal 3 , 20170bc3f43STom Hukins.Xr daemon 8 202044f94adSGordon Bergling.Sh STANDARDS 203044f94adSGordon BerglingThe 204044f94adSGordon Bergling.Nm 205044f94adSGordon Berglingutility is compliant with the 206044f94adSGordon Bergling.St -p1003.1-2024 207044f94adSGordon Berglingspecification. 208e7ab1336SMateusz Piotrowski.Sh HISTORY 209e7ab1336SMateusz PiotrowskiThe 210e7ab1336SMateusz Piotrowski.Nm 211e7ab1336SMateusz Piotrowskicommand first appeared in 212e7ab1336SMateusz Piotrowski.Fx 10.3 . 21306690044SGordon Bergling.Pp 21406690044SGordon BerglingThe 21506690044SGordon Bergling.Fx 21606690044SGordon Berglingwork is compatible with GNU 21706690044SGordon Bergling.Nm 21806690044SGordon Berglingby 21906690044SGordon Bergling.An Padraig Brady , 22006690044SGordon Berglingfrom GNU Coreutils 8.21. 22106690044SGordon BerglingThe 22206690044SGordon Bergling.Nm 22306690044SGordon Berglingutility first appeared in GNU Coreutils 7.0. 224e7ab1336SMateusz Piotrowski.Sh AUTHORS 225e7ab1336SMateusz Piotrowski.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org 226e7ab1336SMateusz Piotrowskiand 227e7ab1336SMateusz Piotrowski.An Vsevolod Stakhov Aq Mt vsevolod@FreeBSD.org 228