From b3b0fc94f9782566977efdddd344f925c7f7522b Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Sat, 15 Dec 2012 00:28:59 +0100 Subject: [PATCH] crontab.d: Add, directory for crontab snippets. ~/.crontab.d can be used by other configuration files (e.g. vim) to provide crontab entries which are then loaded by `update.sh` in the crontab entry of the current user. --- crontab.d/README | 8 ++++ crontab.d/update.sh | 90 +++++++++++++++++++++++++++++++++++++++++++++ setup.sh | 3 ++ 3 files changed, 101 insertions(+) create mode 100644 crontab.d/README create mode 100755 crontab.d/update.sh diff --git a/crontab.d/README b/crontab.d/README new file mode 100644 index 0000000..7017445 --- /dev/null +++ b/crontab.d/README @@ -0,0 +1,8 @@ +README +====== + +`update.sh` combines all 'crontab.*' files in this directory into a single +crontab file and loads it with `crontab`. + +This replaces the current crontab! If no 'crontab.*' files were found, the +current crontab is removed. diff --git a/crontab.d/update.sh b/crontab.d/update.sh new file mode 100755 index 0000000..cfdcbcc --- /dev/null +++ b/crontab.d/update.sh @@ -0,0 +1,90 @@ +#!/bin/sh + +# Combine all crontab.* files in ~/.crontab.d/ into a single crontab file and +# load it with `crontab`. +# +# An existing crontab entry not generated with this script is not overwritten. + +# Copyright (C) 2012 Simon Ruderich +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + + +set -e + + +HEADER_WARNING="# WARNING! DO NOT EDIT THIS FILE! #" + +# Don't overwrite existing crontab entries. +if crontab -l >/dev/null 2>&1; then + if crontab -l | head -n3 | grep "^$HEADER_WARNING$" >/dev/null; then + : + else + echo "Existing crontab entry found, please remove it manually." + exit 2 + fi +fi + + +DIRECTORY="$HOME/.crontab.d" +if test ! -d "$DIRECTORY" || test ! -O "$DIRECTORY"; then + exit 1 +fi + +CRONTAB=`mktemp --tmpdir="$DIRECTORY" update-crontab.XXXXXXXXXXXX` + +echo "###################################" > "$CRONTAB" +echo "$HEADER_WARNING" >> "$CRONTAB" +echo "###################################" >> "$CRONTAB" +echo >> "$CRONTAB" +echo "# It was generated from '$DIRECTORY/*' on `date -R`." >> "$CRONTAB" + + +NO_MATCHES= +for file in "$DIRECTORY"/crontab.*; do + # No crontab.* files exist, abort. + if test ! -e "$file"; then + NO_MATCHES=y + break + fi + + echo "Found '$file'." + + echo >> "$CRONTAB" + echo >> "$CRONTAB" + echo "## $file:" >> "$CRONTAB" + + START_REGEX='^# This [a-zA-Z]* is free software: you can redistribute it' + END_REGEX='^# along with this [a-zA-Z]*. If not, see <[^>]*>\.' + < "$file" sed -e "/$START_REGEX/,/$END_REGEX/ d" \ + -e "/^#[ ]*$/ d" \ + -e "/^# Copyright (C) [0-9][0-9]* / d" \ + | cat --squeeze-blank \ + >> "$CRONTAB" +done + +# Update crontab with the crontab.* files. +if test -z "$NO_MATCHES"; then + echo + echo 'Updating crontab.' + crontab "$CRONTAB" + +# No files found, remove the existing crontab entry. Ignore errors in case no +# crontab entry existed in the first place. +else + echo 'No files found, removing old crontab (if it exists).' + crontab -r || true +fi + +rm "$CRONTAB" diff --git a/setup.sh b/setup.sh index d8c00fd..adb2721 100755 --- a/setup.sh +++ b/setup.sh @@ -226,6 +226,9 @@ link zsh/rc ~/.zshrc link zsh/logout ~/.zlogout # Link setup for additional files. +if installed crontab; then + link crontab.d ~/.crontab.d +fi link lessfilter ~/.lessfilter if installed colordiff; then link colordiffrc ~/.colordiffrc -- 2.44.1