Skip to contentSkip to conversation

crontabs: Provide way to install crontabs, with a safety catch

PATCH In review Ian Jackson 2 participants 2 messages Sep 28, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crontabs: Provide way to install crontabs, with a safety catch

With this setup you can say
./mg-crontab-install CRONTAB
or even
./crontab
./crontab-cambridge

And you can't accidentally install the intended crontab on the wrong host, etc. (As I did recently!)

---
 crontab            |    3 +++
 crontab-cambridge  |    3 +++
 mg-crontab-install |   28 ++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+)
 mode change 100644 => 100755 crontab
 mode change 100644 => 100755 crontab-cambridge
 create mode 100755 mg-crontab-install
crontab +3 -0
old mode 100644
new mode 100755
index b9d4ad1..6fd5704
@@ -1,3 +1,6 @@
+#!/bin/bash ./mg-crontab-install
+#@@ [email protected]
+
 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 [email protected],[email protected]
 # m		h	dom mon dow	command
crontab-cambridge +3 -0
old mode 100644
new mode 100755
index b45c0dd..2ababb0
@@ -1,3 +1,6 @@
+#!/bin/bash ./mg-crontab-install
+#@@ [email protected]
+
 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 [email protected],[email protected]
 # m		h	dom mon dow	command
mg-crontab-install new file +28 -0
new file mode 100755
index 0000000..5a59d45
@@ -0,0 +1,28 @@
+#!/bin/bash
+# usage: ./mg-crontab-install CRONTAB
+#
+# where CRONTAB is a file containing a line
+#  #@@ USER@HOSTFQDN
+
+set -e -o posix
+
+case "$#.$1" in
+1.[^-]*) crontab="$1"; shift ;;
+*) echo >&2 "bad usage"; exit 1 ;;
+esac
+
+expect="#@@ "
+expect+="`whoami`"
+expect+="@"
+expect+="`hostname -f`"
+
+if ! grep -xF "$expect" "$crontab" >/dev/null; then
+	exec >&2
+	echo "running as: $expect"
+	printf "file is for: "
+	grep '^#@@' "$crontab" || echo "no #@@ line?"
+	exit 2
+fi
+
+crontab "$crontab"
+echo "crontab installed ($expect)"
Signature · 1 line
1.7.10.4