Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/at/crontab.sh
1808 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1996-2011 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# Glenn Fowler <[email protected]> #
18
# #
19
########################################################################
20
:
21
# Glenn Fowler
22
# AT&T Research
23
24
command=crontab
25
prefix=${0##*/}
26
case $prefix in
27
*_*) prefix=${prefix%%_*}_ ;;
28
*) prefix= ;;
29
esac
30
cron="${prefix}at -qc"
31
file=$HOME/.$command
32
replace=
33
temp=${TMPDIR:-/tmp}/cron$$
34
op=
35
36
case $(getopts '[-][123:xyz]' opt --xyz 2>/dev/null; echo 0$opt) in
37
0123) usage=$'
38
[-?
39
@(#)$Id: crontab (AT&T Labs Research) 2006-05-17 $
40
]
41
'$USAGE_LICENSE$'
42
[+NAME?crontab - schedule periodic background work]
43
[+DESCRIPTION?\bcrontab\b creates, replaces or edits the calling user crontab
44
entry; a crontab entry is a list of commands and the times at which
45
they are to be executed. The new crontab entry can be input by
46
specifying \afile\a, or input from standard input if \afile\a is
47
omitted, or by using an editor, if \b--edit\b is specified. The
48
actual crontab entry is maintained in the file \b$HOME/.crontab\b.]
49
[+?Upon execution of a command from a crontab entry, the implementation will
50
supply a default environment, defining at least the following
51
environment variables:]{
52
[+HOME?A pathname of the calling user home directory.]
53
[+LOGNAME?The calling user login name.]
54
[+PATH?A string representing a search path guaranteed to find all
55
of the standard utilities.]
56
[+SHELL?A pathname of the command interpreter \bsh\b(1).]
57
}
58
[+?If standard output and standard error are not redirected by commands
59
executed from the crontab entry, any generated output or errors will
60
be mailed to the calling user.]
61
[+?Users are permitted to use \bcrontab\b if their names appear in the file
62
\b/usr/lib/cron/cron.allow\b. If that file does not exist, the file
63
\b/usr/lib/cron/cron.deny\b is checked to determine if the calling user
64
should be denied access to \bcrontab\b. If neither file exists, only
65
a process with appropriate privileges is allowed to submit a job. If
66
only \bcron.deny\b exists and is empty, global usage is permitted. The
67
\bcron.allow\b and \bcron.deny\b files consist of one user name per
68
line.]
69
[+?In this implementation, each command in the \bcrontab\b file represents
70
a single \bat\b(1) \ajob\a. Only one job per command will be executed.
71
A new job for a given command will not be scheduled until the previous
72
job for that command, if any, has exited.]
73
[e:edit?Edit a copy of the calling user crontab entry, or create an empty
74
entry to edit if the crontab entry does not exist. When editing is
75
complete, the entry will be installed as the calling user crontab
76
entry. The editor used is defined by the \bEDITOR\b environment
77
variable. The default editor is \bvi\b(1).]
78
[l:list?List the calling user crontab entry.]
79
[r:remove|delete?Remove the calling user crontab entry.]
80
81
[ file ]
82
83
[+INPUT FILES?A crontab entry must be a text file consisting of lines of six
84
fields each. The fields must be separated by blank characters. The
85
first five fields must be integer patterns that specify the
86
following:]{
87
[+1.?Minute (0-59)]
88
[+2.?Hour (0-23)]
89
[+3.?Day of the month (1-31)]
90
[+4.?Month of the year (1-12)]
91
[+5.?Day of the week (0-6 with 0=Sunday)]
92
}
93
[+?Each of these patterns can be either an asterisk (meaning all valid
94
values), an element or a list of elements separated by commas.
95
An element must be either a number or two numbers separated by a
96
hyphen (meaning an inclusive range). The specification of days can
97
be made by two fields (day of the month and day of the week). If
98
month, day of month and day of week are all asterisks, every day
99
will be matched. If either the month or day of month is specified
100
as an element or list, but the day of week is an asterisk, the
101
month and day of month fields will specify the days that match. If
102
both month and day of month are specified as asterisk, but day of
103
week is an element or list, then only the specified days of the
104
week will match. Finally, if either the month or day of month is
105
specified as an element or list, and the day of week is also
106
specified as an element or list, then any day matching either the
107
month and day of month or the day of week, will be matched.]
108
[+?The sixth field of a line in a crontab entry is a string that will
109
be executed by \bsh\b at the specified times. A percent sign character
110
in this field will be translated to a newline character. Any
111
character preceded by a backslash (including the %) causes that
112
character to be treated literally. Only the first line (up to a "%"
113
or end-of-line) of the command field will be executed by the
114
command interpreter. The other lines will be made available to the
115
command as standard input.]
116
[+?A job label is generated by a readable hash of the command. If the first
117
characters are \b:\b \alabel\a\b;\b then the job label will be set to
118
\alabel\a verbatim. Job labels are limited to 12 characters.]
119
[+?Blank lines and those whose first non-blank character is "#" will
120
be ignored.]
121
[+FILES]{
122
[+$HOME/.crontab?Edited per-user crontab entry file.]
123
}
124
[+SEE ALSO?\bat\b(1), \bbatch\b(1), \bsh\b(1)]
125
'
126
;;
127
*) usage='elr [ file ]'
128
;;
129
esac
130
131
# check the options
132
133
while getopts "$usage" OPT
134
do case $OPT in
135
e) op=edit ;;
136
l) op=list ;;
137
r) op=remove ;;
138
*) echo "Usage: $command [-elr] [ file ]" >&2
139
exit 2
140
;;
141
esac
142
done
143
shift $((OPTIND-1))
144
case $# in
145
0) ;;
146
1) replace=$1; shift ;;
147
esac
148
case $op in
149
'') op=replace ;;
150
*) case $replace in
151
?*) set -- '-?' ;;
152
esac
153
;;
154
esac
155
case $# in
156
0) ;;
157
*) getopts "$usage" OPT; exit 2 ;;
158
esac
159
set -o errexit -o noglob
160
161
# check for cron queue access
162
163
$cron -a
164
165
# do the operation keeping cron entry private
166
167
umask 077
168
case $op in
169
edit) trap "rm -f '$temp'" 0 1 2 3 15
170
if test -f "$file"
171
then cat "$file"
172
else print "# MIN HOUR DAY/MONTH MONTH DAY/WEEK COMMAND"
173
fi > "$temp"
174
${VISUAL:-${EDITOR:-vi}} "$temp"
175
;;
176
list) if test -f "$file"
177
then cat "$file"
178
fi
179
exit
180
;;
181
remove) $cron -r
182
rm -f "$file"
183
exit
184
;;
185
replace)trap "rm -f '$temp'" 0 1 2 3 15
186
case $replace in
187
-) temp=$file ;;
188
"") cat > "$temp" ;;
189
*) cat "$replace" > "$temp" ;;
190
esac
191
test -s "$temp"
192
;;
193
esac
194
exec > /dev/null
195
196
# verify the cron entries (no update unless all ok)
197
198
while read -r line
199
do set '' $line
200
shift
201
case $1 in
202
''|'#'*)continue ;;
203
esac
204
case $# in
205
[0-5]) print -u2 "$command: $line: invalid entry"
206
esac
207
$cron -n -t "cron $1 $2 $3 $4 $5" true
208
done < "$temp"
209
210
# update the cron entries
211
212
$cron -r
213
while read -r line
214
do set '' $line
215
shift
216
case $1 in
217
''|'#'*)continue ;;
218
esac
219
time="$1 $2 $3 $4 $5"
220
shift 5
221
exec=${*//\\(?)/\1}
222
exec=${exec//@([!])%/\1
223
}
224
exec=${exec//^A/\\}
225
exec=${exec//@(
226
*)/\<\<\\
227
\1
228
}
229
$cron -t "cron $time" "$exec" 2> /dev/null
230
done < "$temp"
231
case $replace in
232
-) ;;
233
*) cp "$temp" "$file" ;;
234
esac
235
236