xref: /freebsd/bin/timeout/timeout.1 (revision 8a9927ecd4b6e8cb306e4b940201b4efbfbc5dcb)
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 2, 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 options are as follows:
69.Bl -tag -width indent
70.It Fl f , Fl -foreground
71Only time out the
72.Ar command
73itself, but do not propagate signals to its descendants.
74.It Fl k Ar time , Fl -kill-after Ar time
75Send a
76.Dv SIGKILL
77signal if
78.Ar command
79is still running after
80.Ar time
81since the first signal was sent.
82.It Fl p , Fl -preserve-status
83Always exit with the same status as
84.Ar command ,
85even if the timeout was reached.
86.It Fl s Ar signal , Fl -signal Ar signal
87Specify the signal to send on timeout.
88By default,
89.Dv SIGTERM
90is sent.
91.It Fl v , Fl -verbose
92Show information to stderr about any signal sent on timeout.
93.El
94.Ss Duration Format
95The
96.Ar duration
97and
98.Ar time
99are non-negative integer or real (decimal) numbers, with an optional
100suffix specifying the unit.
101Values without an explicit unit are interpreted as seconds.
102.Pp
103Supported unit suffixes are:
104.Bl -tag -offset indent -width indent -compact
105.It Cm s
106seconds
107.It Cm m
108minutes
109.It Cm h
110hours
111.It Cm d
112days
113.El
114.Sh EXIT STATUS
115If the timeout was not reached, the exit status of
116.Ar command
117is returned.
118.Pp
119If the timeout was reached and
120.Fl -preserve-status
121is set, the exit status of
122.Ar command
123is returned.
124If
125.Fl -preserve-status
126is not set, an exit status of 124 is returned.
127.Pp
128If an invalid parameter is passed to
129.Fl s
130or
131.Fl k ,
132the exit status returned is 125.
133.Pp
134If
135.Ar command
136is an otherwise invalid program, the exit status returned is 126.
137.Pp
138If
139.Ar command
140refers to a non-existing program, the exit status returned is 127.
141.Pp
142If
143.Ar command
144exits after receiving a signal, the exit status returned is the signal number
145plus 128.
146.Sh EXAMPLES
147Run
148.Xr sleep 1
149with a time limit of 4 seconds.
150Since the command completes in 2 seconds, the exit status is 0:
151.Bd -literal -offset indent
152$ timeout 4 sleep 2
153$ echo $?
1540
155.Ed
156.Pp
157Run
158.Xr sleep 1
159for 4 seconds and terminate process after 2 seconds.
160The exit status is 124 since
161.Fl -preserve-status
162is not used:
163.Bd -literal -offset indent
164$ timeout 2 sleep 4
165$ echo $?
166124
167.Ed
168.Pp
169Same as above but preserving status.
170The exit status is 128 + signal number (15 for
171.Dv SIGTERM ) :
172.Bd -literal -offset indent
173$ timeout --preserve-status 2 sleep 4
174$ echo $?
175143
176.Ed
177.Pp
178Same as above but sending
179.Dv SIGALRM
180(signal number 14) instead of
181.Dv SIGTERM :
182.Bd -literal -offset indent
183$ timeout --preserve-status -s SIGALRM 2 sleep 4
184$ echo $?
185142
186.Ed
187.Pp
188Try to
189.Xr fetch 1
190the PDF version of the
191.Fx
192Handbook.
193Send a
194.Dv SIGTERM
195signal after 1 minute and send a
196.Dv SIGKILL
197signal 5 seconds later if the process refuses to stop:
198.Bd -literal -offset indent
199$ timeout -k 5s 1m fetch \\
200> https://download.freebsd.org/ftp/doc/en/books/handbook/book.pdf
201.Ed
202.Sh SEE ALSO
203.Xr kill 1 ,
204.Xr nohup 1 ,
205.Xr signal 3 ,
206.Xr daemon 8
207.Sh STANDARDS
208The
209.Nm
210utility is expected to conform to the
211.St -p1003.1-2024
212specification.
213.Sh HISTORY
214The
215.Nm
216command first appeared in
217.Fx 10.3 .
218.Pp
219The
220.Fx
221work is compatible with GNU
222.Nm
223by
224.An Padraig Brady ,
225from GNU Coreutils 8.21.
226The
227.Nm
228utility first appeared in GNU Coreutils 7.0.
229.Sh AUTHORS
230.An Baptiste Daroussin Aq Mt bapt@FreeBSD.org
231and
232.An Vsevolod Stakhov Aq Mt vsevolod@FreeBSD.org
233