xref: /openbsd/usr.sbin/cron/crontab.5 (revision d415bd75)
1.\"/* Copyright 1988,1990,1993,1994 by Paul Vixie
2.\" * All rights reserved
3.\" */
4.\"
5.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
6.\" Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
7.\"
8.\" Permission to use, copy, modify, and distribute this software for any
9.\" purpose with or without fee is hereby granted, provided that the above
10.\" copyright notice and this permission notice appear in all copies.
11.\"
12.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
13.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14.\" MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
15.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
18.\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19.\"
20.\" $OpenBSD: crontab.5,v 1.42 2023/05/06 23:06:27 millert Exp $
21.\"
22.Dd $Mdocdate: May 6 2023 $
23.Dt CRONTAB 5
24.Os
25.Sh NAME
26.Nm crontab
27.Nd tables for driving cron
28.Sh DESCRIPTION
29A
30.Nm
31file contains instructions to the
32.Xr cron 8
33daemon of the general form:
34.Dq at these times on these dates run this command .
35There may be a system
36.Nm
37and each user may have their own
38.Nm .
39Commands in any given
40.Nm
41will be
42executed either as the user who owns the
43.Nm
44or, in the case of the system
45.Nm crontab ,
46as the user specified on the command line.
47.Pp
48While a
49.Nm
50is a text file, it is not intended to be directly edited.
51Creation, modification, and removal of a
52.Nm
53should be done using
54.Xr crontab 1 .
55.Pp
56Blank lines, leading spaces, and tabs are ignored.
57Lines whose first non-space character is a pound sign
58.Pq Ql #
59are comments, and are ignored.
60Note that comments are not allowed on the same line as
61.Xr cron 8
62commands, since
63they will be taken to be part of the command.
64Similarly, comments are not
65allowed on the same line as environment variable settings.
66.Pp
67An active line in a
68.Nm
69is either an environment variable setting or a
70.Xr cron 8
71command.
72.Pp
73Environment variable settings create the environment
74any command in the
75.Nm
76is run in.
77An environment variable setting is of the form:
78.Pp
79.Dl name = value
80.Pp
81The spaces around the equal sign
82.Pq Ql =
83are optional, and any subsequent non-leading spaces in
84.Ar value
85will be part of the value assigned to
86.Ar name .
87The
88.Ar value
89string may be placed in quotes
90.Pq single or double , but matching
91to preserve leading or trailing blanks.
92.Pp
93Lines in the system
94.Nm
95have six fixed fields, an optional flags field, and a command, in the form:
96.Bd -ragged -offset indent
97.Ar minute
98.Ar hour
99.Ar day-of-month
100.Ar month
101.Ar day-of-week
102.Ar user
103.Op Ar flags
104.Ar command
105.Ed
106.Pp
107While lines in a user
108.Nm
109have five fixed fields, an optional flags field, and a command, in the form:
110.Bd -ragged -offset indent
111.Ar minute
112.Ar hour
113.Ar day-of-month
114.Ar month
115.Ar day-of-week
116.Op Ar flags
117.Ar command
118.Ed
119.Pp
120Fields are separated by blanks or tabs.
121The command may be one or more fields long.
122The allowed values for the fields are:
123.Bl -column "day-of-month" "allowed values" -offset indent
124.It Sy field Ta Sy allowed values
125.It Ar minute Ta * or 0\(en59
126.It Ar hour Ta * or 0\(en23
127.It Ar day-of-month Ta * or 1\(en31
128.It Ar month Ta * or 1\(en12 or a name (see below)
129.It Ar day-of-week Ta * or 0\(en7 or a name (0 or 7 is Sunday)
130.It Ar user Ta a valid username
131.It Op Ar flags Ta runtime flags, denoted with '-'
132.It Ar command Ta text
133.El
134.Pp
135Lists are allowed.
136A list is a set of numbers (or ranges) separated by commas.
137For example,
138.Dq 1,2,5,9
139or
140.Dq 0\(en4,8\(en12 .
141.Pp
142Ranges of numbers are allowed.
143Ranges are two numbers separated with a hyphen.
144The specified range is inclusive.
145For example,
1468\(en11 for an
147.Ar hour
148entry specifies execution at hours 8, 9, 10 and 11.
149.Pp
150A random value (within the legal range) may be obtained by using the
151.Ql ~
152character in a field.
153The interval of the random value may be specified explicitly, for example
154.Dq 0~30
155will result in a random value between 0 and 30 inclusive.
156If either (or both) of the numbers on either side of the
157.Ql ~
158are omitted, the appropriate limit (low or high) for the field will be used.
159.Pp
160Step values can be used in conjunction with ranges.
161Following a range with
162.No / Ns Ar number
163specifies skips of
164.Ar number
165through the range.
166For example,
167.Dq 0\(en23/2
168can be used in the
169.Ar hour
170field to specify command execution every other hour.
171Steps are also permitted after an asterisk, so to say
172.Dq every two hours ,
173just use
174.Dq */2 .
175A step value after a random range will execute the command at a random
176offset less than the step size.
177For example, to avoid a thundering herd at the top and bottom of the hour,
178.Dq 0~59/30
179.Po
180or simply
181.Dq ~/30
182.Pc
183can be used in the
184.Ar minute
185field to specify that command execution happen twice an hour at
186consistent intervals.
187.Pp
188An asterisk
189.Pq Ql *
190is short form for a range of all allowed values.
191.Pp
192Names can be used in the
193.Ar month
194and
195.Ar day-of-week
196fields.
197Use the first three letters of the particular
198day or month (case doesn't matter).
199Ranges or lists of names are not allowed.
200.Pp
201Some
202.Ar flags
203relating to process operation can be provided before the
204.Ar command
205field.
206Flags are denoted with '-' and may be combined.
207.Bl -tag -width Ds
208.It Fl n Ar command
209No mail is sent after a successful run.
210The execution output will only be mailed if the command exits with a non-zero
211exit code.
212The
213.Fl n
214option is an attempt to cure potentially copious volumes of mail coming from
215.Xr cron 8 .
216.It Fl q Ar command
217Execution will not be logged.
218.It Fl s Ar command
219Only a single instance of
220.Ar command
221will be run concurrently.
222Additional instances of
223.Ar command
224will not be scheduled until the earlier one completes.
225.El
226.Pp
227The
228.Ar command
229field (the rest of the line) is the command to be
230run.
231The entire command portion of the line, up to a newline or %
232character, will be executed by
233.Pa /bin/sh
234or by the shell
235specified in the
236.Ev SHELL
237variable of the
238.Nm crontab .
239Percent signs
240.Pq Ql %
241in the command, unless escaped with a backslash
242.Pq Ql \e ,
243will be changed into newline characters, and all data
244after the first
245.Ql %
246will be sent to the command as standard input.
247.Pp
248Commands are executed by
249.Xr cron 8
250when the
251.Ar minute ,
252.Ar hour ,
253and
254.Ar month
255fields match the current time,
256.Em and
257when at least one of the two day fields
258.Po Ar day-of-month
259or
260.Ar day-of-week Pc ,
261match the current time.
262.Pp
263Note: The day of a command's execution can be specified by two
264fields \(em
265.Ar day-of-month
266and
267.Ar day-of-week .
268If both fields are restricted (i.e. aren't *),
269the command will be run when
270.Em either
271field matches the current time.
272For example,
273.Pp
274.Dl 30 4 1,15 * 5
275.Pp
276would cause a command to be run at 4:30 am on the 1st and 15th of each
277month, plus every Friday.
278.Pp
279Instead of the first five fields, one of eight special strings may appear:
280.Bl -column "@midnight" "meaning" -offset indent
281.It Sy string Ta Sy meaning
282.It @reboot Ta Run once, at startup.
283.It @yearly Ta Run every January 1 (0 0 1 1 *).
284.It @annually Ta The same as @yearly.
285.It @monthly Ta Run the first day of every month (0 0 1 * *).
286.It @weekly Ta Run every Sunday (0 0 * * 0).
287.It @daily Ta Run every midnight (0 0 * * *).
288.It @midnight Ta The same as @daily.
289.It @hourly Ta Run every hour, on the hour (0 * * * *).
290.El
291.Sh ENVIRONMENT
292.Bl -tag -width "LOGNAMEXXX"
293.It Ev HOME
294Set from the user's
295.Pa /etc/passwd
296entry.
297May be overridden by settings in the
298.Nm .
299.It Ev LOGNAME
300Set from the user's
301.Pa /etc/passwd
302entry.
303May not be overridden by settings in the
304.Nm .
305.It Ev MAILTO
306If
307.Ev MAILTO
308is defined and non-empty,
309mail is sent to the user so named.
310If
311.Ev MAILTO
312is defined but empty
313.Pq Ev MAILTO = Qq ,
314no mail will be sent.
315Otherwise mail is sent to the owner of the
316.Nm .
317This is useful for pseudo-users that lack an alias
318that would otherwise redirect the mail to a real person.
319.It Ev SHELL
320Set to
321.Pa /bin/sh .
322May be overridden by settings in the
323.Nm .
324.It Ev USER
325Set from the user's
326.Pa /etc/passwd
327entry.
328May not be overridden by settings in the
329.Nm .
330.El
331.Sh FILES
332.Bl -tag -width "/var/cron/tabs/<user>XXX" -compact
333.It Pa /etc/crontab
334System crontab.
335.It Pa /var/cron/tabs/ Ns Aq Ar user
336User crontab.
337.El
338.Sh EXAMPLES
339.Bd -literal
340# use /bin/sh to run commands, no matter what /etc/passwd says
341SHELL=/bin/sh
342# mail any output to `paul', no matter whose crontab this is
343MAILTO=paul
344#
345# run five minutes after midnight, every day
3465 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
347
348# run at 2:15pm on the first of every month -- job output will be sent
349# to paul, but only if $HOME/bin/monthly exits with a non-zero exit code
35015 14 1 * *     -n $HOME/bin/monthly
351
352# run at 10 pm on weekdays, annoy Joe
3530 22 * * 1-5	mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
354
35523 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
356
3575 4 * * sun     echo "run at 5 after 4 every sunday"
358
359# run hourly at a random time within the first 30 minutes of the hour
3600~30 * * * *   /usr/libexec/spamd-setup
361.Ed
362.Sh SEE ALSO
363.Xr crontab 1 ,
364.Xr cron 8
365.Sh STANDARDS
366The
367.Nm
368file format is compliant with the
369.St -p1003.1-2008
370specification.
371The behaviours described below are all extensions to that standard:
372.Bl -dash
373.It
374The
375.Ar day-of-week
376field may use 7 to represent Sunday.
377.It
378Ranges may include
379.Dq steps .
380.It
381Random intervals are supported using the
382.Ql ~
383character.
384.It
385Months or days of the week can be specified by name.
386.It
387Environment variables can be set in a crontab.
388.It
389Command output can be mailed to a person other than the crontab
390owner, or the feature can be turned off and no mail will be sent
391at all.
392.It
393All of the
394.Ql @
395commands that can appear in place of the first five fields.
396.It
397All of the
398.Op Fl nqs
399flags.
400.El
401.Sh AUTHORS
402.Nm
403was written by
404.An Paul Vixie Aq Mt vixie@isc.org .
405