3 # Combine all crontab.* files in ~/.crontab.d/ into a single crontab file and
4 # load it with `crontab`.
6 # An existing crontab entry not generated with this script is not overwritten.
8 # Copyright (C) 2012 Simon Ruderich
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 HEADER_WARNING="# WARNING! DO NOT EDIT THIS FILE! #"
29 # Don't overwrite existing crontab entries.
30 if crontab -l >/dev/null 2>&1; then
31 if crontab -l | head -n3 | grep "^$HEADER_WARNING$" >/dev/null; then
34 echo "Existing crontab entry found, please remove it manually."
40 DIRECTORY="$HOME/.crontab.d"
41 if test ! -d "$DIRECTORY" || test ! -O "$DIRECTORY"; then
45 CRONTAB=`mktemp --tmpdir="$DIRECTORY" update-crontab.XXXXXXXXXXXX`
47 echo "###################################" > "$CRONTAB"
48 echo "$HEADER_WARNING" >> "$CRONTAB"
49 echo "###################################" >> "$CRONTAB"
51 echo "# It was generated from '$DIRECTORY/*' on `date -R`." >> "$CRONTAB"
55 for file in "$DIRECTORY"/crontab.*; do
56 # No crontab.* files exist, abort.
57 if test ! -e "$file"; then
66 echo "## $file:" >> "$CRONTAB"
68 START_REGEX='^# This [a-zA-Z]* is free software: you can redistribute it'
69 END_REGEX='^# along with this [a-zA-Z]*. If not, see <[^>]*>\.'
70 < "$file" sed -e "/$START_REGEX/,/$END_REGEX/ d" \
72 -e "/^# Copyright (C) [0-9][0-9]* / d" \
73 | cat --squeeze-blank \
77 # Update crontab with the crontab.* files.
78 if test -z "$NO_MATCHES"; then
80 echo 'Updating crontab.'
83 # No files found, remove the existing crontab entry. Ignore errors in case no
84 # crontab entry existed in the first place.
86 echo 'No files found, removing old crontab (if it exists).'