Merge branch 'master' of https://git.jamestechnotes.com/r/james/home/home
[james-home.git] / bin / loadcron
blob07c4cdd899d801ef589dde235bc0c3a8355290e5
1 #!/bin/sh
2 # Construct a crontab based on files in ~/.cron and load it into cron.
3 # The file ~/.cron/username/fqdn is appended to the common file in the same
4 # directory. Note that the common file may have "$HOME" in it, as in
5 # "PATH=$HOME/bin". cron is too dumb to deal with that, so the expansion is
6 # handled by this script.
7 set -e
9 which crontab >/dev/null 2>&1 || exit 0
11 WARNING="# Automatically generated by loadcron; edit ~/.cron/ files instead."
13 if [ ! -z "`crontab -l`" ] && ! crontab -l | grep -q "$WARNING"; then
14 if [ "$1" != "-f" ]; then
15 echo "loadcron: Current crontab was not generated by loadcron; not changing." >&2
16 echo "loadcron: Use loadcron -f to override"
17 exit 1
18 else
19 crontab -l > $HOME/tmp/oldcrontab
20 echo "loadcron: Old crontab is backed up to $HOME/tmp/oldcrontab"
24 dir=$HOME/.cron-stage/`whoami`
25 if [ -d "$dir" ]; then
26 hostfile="$dir/`hostname -f`"
27 if [ ! -e "$hostfile" ]; then
28 echo "loadcron: $hostfile does not exist, not loading" >&2
29 exit 0
33 echo "$WARNING"
34 echo
35 if [ -e "$dir/common" ]; then
36 echo "# From $dir/common:"
37 sed "s!\$HOME!$HOME!" < "$dir/common"
38 echo
40 hostfile="$dir/`hostname -f`"
41 if [ -e "$hostfile" ]; then
42 echo "# From $hostfile:"
43 cat "$hostfile"
44 echo
46 ) | crontab -