uscrontab: possibilité de mettre en pause sans modifier les planifications en cours

This commit is contained in:
Jephté Clain 2015-10-27 12:05:00 +04:00
parent 0eeee20c7a
commit 2118885e43
2 changed files with 79 additions and 5 deletions

View File

@ -92,7 +92,12 @@ function enable_in_crontab() {
}
# Afficher la spécification crontab correspondant à l'heure courante
function ctnow() {
date +"%-M %-H %-d %-m %u"
#date +"%-M %-H %-d %-m %u"
if [ -n "$1" ]; then
echo "$1" | awk '{ print strftime("%-M %-H %-d %-m %u", $0) }'
else
awk 'BEGIN { print strftime("%-M %-H %-d %-m %u", systime()) }'
fi
}
__CTRESOLVE_CTNOW=""
# Analyser STDIN qui contient des lignes au format crontab, et afficher des

View File

@ -236,6 +236,18 @@ OPTIONS
$USCRONTAB_USERDIR
-n, --fake
Afficher au lieu de les exécuter les commandes qui doivent être lancées
-P, --pause-for NBMINS
Désactiver les planifications pendant NBMINS minutes. Utiliser -1 pour
désactiver les planifications sans limite de durée. Pendant la période
de pause, toutes les invocations de uscrontab n'ont aucun effet, sauf si
on utilise l'option --force
-Y, --unpause
Réactiver les planifications après une mise en pause
-p, --pause
Désactiver les planifications pendant 1 journée. Equivalent à -P 1440
-f, --force
Forcer l'exécution de la planification, même si elle a été mise en pause
avec l'option --pause
OPTIONS AVANCEES
--lock LOCKFILE
@ -298,6 +310,21 @@ function get_usercrontab() {
fi
}
function check_pause() {
# retourner 0 si on est en pause
local pauseuntil pausefile="$pausefile" now="$2"
if [ -f "$pausefile" ]; then
pauseuntil="$(<"$pausefile")"
if [ -z "$pauseuntil" ]; then
# pause sans limitation de durée
return 0
elif [ "$pauseuntil" -gt "$now" ]; then
return 0
fi
fi
return 1
}
USCRONTAB_CTLINE="* * * * * $script"
USCRONTAB_LOCKDELAY=8
USCRONTAB_STOPEC=101
@ -312,6 +339,8 @@ lockfile=auto
lockdelay=
fake=
continuous=
pause=
force=
parse_opts "${PRETTYOPTS[@]}" \
--help '$exit_with display_help' \
-A,--add,--install action=install \
@ -322,6 +351,10 @@ parse_opts "${PRETTYOPTS[@]}" \
-r,--remove action=remove \
-l,--list action=list \
-n,--fake fake=1 \
-P:,--pause-for: '$action=pause; set@ pause' \
-p,--pause action=pause \
-Y,--unpause action=unpause \
-f,--force force=1 \
--lock: lockfile= \
--lockdelay: lockdelay= \
-c,--continuous continuous=1 \
@ -332,7 +365,28 @@ parse_opts "${PRETTYOPTS[@]}" \
uscrontab="$1"; shift
if [ "$action" == "edit" ]; then
pausefile="$USCRONTAB_USERFILE.pauseuntil"
now="$(awk 'BEGIN { print int(systime() / 60) * 60 }')"
if [ "$action" == "pause" ]; then
[ -n "$pause" ] || pause=1440
if [ "$pause" -lt 0 ]; then
pauseuntil=
else
pauseuntil=$(($now + $pause * 60))
fi
echo "$pauseuntil" >"$pausefile" || die
enote "Désactivation des planifications pour $pause minute(s)"
exit 0
elif [ "$action" == "unpause" ]; then
if [ -f "$pausefile" ]; then
rm "$pausefile" || die
enote "Réactivation des planifications"
fi
exit 0
elif [ "$action" == "edit" ]; then
if [ -z "$uscrontab" ]; then
basedir="$(dirname "$USCRONTAB_USERFILE")"
[ -d "$basedir" ] || die "$basedir: ce répertoire n'existe pas. Vérifiez l'installation de nutools"
@ -375,6 +429,10 @@ elif [ "$action" == "remove" ]; then
exit 0
elif [ "$action" == "list" ]; then
if check_pause "$pausefile" "$now"; then
ewarn "En pause. Réactiver avec $scriptname -Y"
fi
if [ -n "$uscrontab" ]; then
uscrontab="$(abspath "$uscrontab")"
array_from_lines ctfiles "$(crontab -l 2>/dev/null | awkrun script="$script" uscrontab="$uscrontab" '$6 == script && $7 == uscrontab { print $7 }')"
@ -394,9 +452,9 @@ elif [ "$action" == "list" ]; then
array_del ctfiles "#GENERIC"
set_usercrontabs usercrontabs
array_extend ctfiles usercrontabs
ewarn "planification générique désactivée. Vous pouvez la réactiver avec $scriptname -A"
ewarn "Planification générique désactivée. Vous pouvez la réactiver avec $scriptname -A"
elif [ ${#ctfiles[*]} -eq 0 ]; then
einfo "aucune planification en cours"
einfo "Aucune planification en cours"
set_usercrontabs ctfiles
fi
fi
@ -410,7 +468,7 @@ elif [ "$action" == "list" ]; then
exit $r
elif [ "$action" == "show-ctnow" ]; then
ctnow
ctnow "$now"
exit 0
fi
@ -432,6 +490,17 @@ elif [ "$action" == "uninstall" ]; then
fi
elif [ "$action" == "run" ]; then
if [ -n "$force" ]; then
# ne pas tenir compte de l'état de pause
:
elif check_pause "$pausefile" "$now"; then
ewarn "En pause. Réactiver avec $scriptname -Y"
ac_clean "$lockfile"
exit 0
elif [ -f "$pausefile" ]; then
rm "$pausefile"
fi
if [ -n "$uscrontab" ]; then
default_lockfile="/var/run/$scriptname$uscrontab.lock"
else