copying from private repo
This commit is contained in:
120
shell/cron-user-run
Executable file
120
shell/cron-user-run
Executable file
@@ -0,0 +1,120 @@
|
||||
#!/bin/bash
|
||||
|
||||
_helpexit() {
|
||||
echo "# Example cron:"
|
||||
_get_cron_template
|
||||
echo "
|
||||
# Cron run, arg is one of:
|
||||
reboot hourly daily weekly monthly all list help
|
||||
all runs all targets but not reboot
|
||||
"
|
||||
exit 1
|
||||
}
|
||||
|
||||
_get_cron_template() {
|
||||
min=$(( $RANDOM % 20 ))
|
||||
echo "
|
||||
### CRON user run ###
|
||||
@reboot $0 reboot
|
||||
$min * * * * $0 hourly
|
||||
$(( $min + 10 )) 4 * * * $0 daily
|
||||
$(( $min + 20 )) 4 * * 1 $0 weekly
|
||||
$(( $min + 30 )) 4 1 * * $0 monthly
|
||||
### CRON user run END ###"
|
||||
}
|
||||
|
||||
_install() {
|
||||
crontab -l 2>/dev/null | grep -q cron-user-run && {
|
||||
echo "Already installed"
|
||||
return
|
||||
}
|
||||
echo "Installing"
|
||||
crontab -l 2>/dev/null
|
||||
tmpfile=$( mktemp )
|
||||
crontab -l > "$tmpfile" 2>/dev/null
|
||||
set -e
|
||||
_get_cron_template >> "$tmpfile"
|
||||
crontab "$tmpfile"
|
||||
rm "$tmpfile"
|
||||
crontab -l
|
||||
return
|
||||
}
|
||||
|
||||
if [ -z "$1" ];then
|
||||
_helpexit
|
||||
fi
|
||||
# SETUP
|
||||
cd $HOME
|
||||
LOG="$HOME/.log/cron.log"
|
||||
TSF="%Y-%m-%d %H:%M:%S [$1]"
|
||||
CRONBASE="$HOME/.local/etc/cron"
|
||||
mkdir -p "$HOME/.log"
|
||||
for task in hourly daily weekly monthly reboot; do
|
||||
if [[ ! -e "$CRONBASE"/"$task" ]]; then
|
||||
mkdir -p "$CRONBASE"/"$task"
|
||||
fi
|
||||
done
|
||||
if [[ ! -e ~/.log/logrotate.conf ]]; then
|
||||
cat <<EOF > ~/.log/logrotate.conf
|
||||
$HOME/.log/*.log {
|
||||
monthly
|
||||
missingok
|
||||
rotate 12
|
||||
compress
|
||||
delaycompress
|
||||
notifempty
|
||||
}
|
||||
EOF
|
||||
fi
|
||||
if [[ ! -e "$CRONBASE"/daily/000-logrotate ]]; then
|
||||
cat <<EOF > "$CRONBASE"/daily/000-logrotate
|
||||
#!/bin/bash
|
||||
|
||||
/usr/sbin/logrotate -s ~/.log/logrotate.state ~/.log/logrotate.conf
|
||||
EOF
|
||||
chmod +x "$CRONBASE"/daily/000-logrotate
|
||||
fi
|
||||
|
||||
case "$1" in
|
||||
help|-h|--help)
|
||||
_helpexit
|
||||
;;
|
||||
all)
|
||||
for task in hourly daily weekly monthly; do
|
||||
"$0" $task
|
||||
done
|
||||
exit $?
|
||||
;;
|
||||
list)
|
||||
if [[ -e "$LOG" ]]; then
|
||||
echo "===================="
|
||||
echo "Log: $LOG"
|
||||
tail -n 10 "$LOG"
|
||||
echo "===================="
|
||||
fi
|
||||
for task in hourly daily weekly monthly reboot; do
|
||||
if [[ -e "$CRONBASE"."$task" ]]; then
|
||||
echo "$CRONBASE"."$task"
|
||||
ls -1 "$CRONBASE"."$task" | sed 's/^/ /'
|
||||
fi
|
||||
if [[ -e "$CRONBASE"/"$task" ]]; then
|
||||
echo "$CRONBASE"/"$task"
|
||||
ls -1 "$CRONBASE"/"$task" | sed 's/^/ /'
|
||||
fi
|
||||
done
|
||||
exit $?
|
||||
;;
|
||||
install)
|
||||
_install
|
||||
;;
|
||||
hourly|daily|weekly|monthly|reboot)
|
||||
echo "Running user cron $1" | ts -m "$TSF" >> "$LOG"
|
||||
if [[ -e "$CRONBASE"."$task" ]]; then
|
||||
run-parts --report -v "$CRONBASE"."$1" 2>&1 | ts -m "$TSF" >> "$LOG"
|
||||
fi
|
||||
if [[ -e "$CRONBASE"/"$task" ]]; then
|
||||
run-parts --report -v "$CRONBASE"/"$1" 2>&1 | ts -m "$TSF" >> "$LOG"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user