2013-08-27 15:14:44 +04:00
|
|
|
##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
## Gestion du fichier crontab
|
|
|
|
##@cooked nocomments
|
|
|
|
##@require base
|
|
|
|
uprovide crontab
|
|
|
|
urequire base
|
|
|
|
|
|
|
|
# Ajouter la ligne $1 au crontab de l'utilisateur $2
|
|
|
|
# Le code de retour est 123 si la ligne est déjà présente
|
|
|
|
function add_to_crontab() {
|
|
|
|
local -a crontab=(crontab ${2:+-u "$2"})
|
|
|
|
local current="$("${crontab[@]}" -l 2>/dev/null)"
|
|
|
|
local tmpfile
|
|
|
|
ac_set_tmpfile tmpfile
|
|
|
|
if [ -n "$current" ]; then
|
|
|
|
echo "$current" >"$tmpfile"
|
|
|
|
fi
|
|
|
|
local NL=$'\n'
|
|
|
|
if quietgrep -xF "$1$NL#$1" "$tmpfile"; then
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
return 123
|
|
|
|
else
|
|
|
|
echo "$1" >>"$tmpfile"
|
|
|
|
"${crontab[@]}" "$tmpfile"
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Supprimer la ligne $1 du crontab de l'utilisateur $2
|
|
|
|
# Le code de retour est 123 si la ligne a déjà été supprimée
|
|
|
|
function remove_from_crontab() {
|
|
|
|
local -a crontab=(crontab ${2:+-u "$2"})
|
|
|
|
local current="$("${crontab[@]}" -l 2>/dev/null)"
|
|
|
|
local tmpfile
|
|
|
|
ac_set_tmpfile tmpfile
|
|
|
|
if [ -n "$current" ]; then
|
|
|
|
echo "$current" >"$tmpfile"
|
|
|
|
fi
|
|
|
|
local NL=$'\n'
|
|
|
|
if ! quietgrep -xF "$1$NL#$1" "$tmpfile"; then
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
return 123
|
|
|
|
else
|
|
|
|
grep -vxF "$1$NL#$1" "$tmpfile" | "${crontab[@]}" -
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Désactiver la ligne $1 du crontab de l'utilisateur $2 en mettant '#' devant. Si
|
|
|
|
# la ligne n'existe pas, elle est ajoutée mais commentée.
|
|
|
|
# Le code de retour est 123 si la ligne est déjà désactivée
|
|
|
|
function disable_in_crontab() {
|
|
|
|
local -a crontab=(crontab ${2:+-u "$2"})
|
|
|
|
local current="$("${crontab[@]}" -l 2>/dev/null)"
|
|
|
|
local tmpfile
|
|
|
|
ac_set_tmpfile tmpfile
|
|
|
|
if [ -n "$current" ]; then
|
|
|
|
echo "$current" >"$tmpfile"
|
|
|
|
fi
|
|
|
|
local NL=$'\n'
|
|
|
|
if ! quietgrep -xF "$1$NL#$1" "$tmpfile"; then
|
|
|
|
echo "#$1" >>"$tmpfile"
|
|
|
|
"${crontab[@]}" "$tmpfile"
|
|
|
|
elif quietgrep -xF "#$1" "$tmpfile"; then
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
return 123
|
|
|
|
else
|
|
|
|
<"$tmpfile" awkrun line="$1" '$0 == line { $0 = "#" $0 } { print }' | "${crontab[@]}" -
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Activer la ligne $1 du crontab de l'utilisateur $2 en enlevant '#' devant. Si
|
|
|
|
# la ligne n'existe pas, elle est ajoutée.
|
|
|
|
# Le code de retour est 123 si la ligne est déjà activée
|
|
|
|
function enable_in_crontab() {
|
|
|
|
local -a crontab=(crontab ${2:+-u "$2"})
|
|
|
|
local current="$("${crontab[@]}" -l 2>/dev/null)"
|
|
|
|
local tmpfile
|
|
|
|
ac_set_tmpfile tmpfile
|
|
|
|
if [ -n "$current" ]; then
|
|
|
|
echo "$current" >"$tmpfile"
|
|
|
|
fi
|
|
|
|
local NL=$'\n'
|
|
|
|
if ! quietgrep -xF "$1$NL#$1" "$tmpfile"; then
|
|
|
|
echo "$1" >>"$tmpfile"
|
|
|
|
"${crontab[@]}" "$tmpfile"
|
|
|
|
elif quietgrep -xF "$1" "$tmpfile"; then
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
return 123
|
|
|
|
else
|
|
|
|
<"$tmpfile" awkrun line="$1" '$0 == "#" line { sub(/^#/, "") } { print }' | "${crontab[@]}" -
|
|
|
|
ac_clean "$tmpfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Afficher la spécification crontab correspondant à l'heure courante
|
|
|
|
function ctnow() {
|
2015-10-27 12:05:00 +04:00
|
|
|
#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
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
__CTRESOLVE_CTNOW=""
|
2013-08-27 15:14:44 +04:00
|
|
|
# Analyser STDIN qui contient des lignes au format crontab, et afficher des
|
|
|
|
# commandes pour les lignes correspondant à la date courante.
|
|
|
|
# Les commandes sont de la forme "export var=value" pour la définition d'une
|
|
|
|
# variable, "__ctexec 'cmd'" pour exécuter la commande correspondante ou
|
|
|
|
# "__cterror 'msg'" en cas d'erreur de syntaxe sur une ligne. Il faut bien
|
|
|
|
# entendu définir les function __ctexec et __cterror.
|
|
|
|
# Cette fonction s'utilise de cette manière:
|
|
|
|
# function __ctexec() { eval "$*"; }
|
|
|
|
# function __cterror() { die "$*"; }
|
|
|
|
# eval "$(ctresolve <crontab)"
|
|
|
|
function ctresolve() {
|
|
|
|
local -a ctnow
|
2017-02-20 13:55:49 +04:00
|
|
|
if [ "$__CTRESOLVE_CTNOW" == "**ANY**" ]; then
|
|
|
|
ctnow=("$__CTRESOLVE_CTNOW")
|
|
|
|
elif [ -n "$__CTRESOLVE_CTNOW" ]; then
|
2014-08-20 00:12:36 +04:00
|
|
|
eval "ctnow=($__CTRESOLVE_CTNOW)"
|
|
|
|
else
|
|
|
|
eval "ctnow=($(ctnow))"
|
|
|
|
fi
|
2013-08-27 15:14:44 +04:00
|
|
|
filter_conf | awkrun -f ctnow[@] '
|
2014-08-20 00:12:36 +04:00
|
|
|
function ctmatch_one(ctval, ref, parts, part, i, j, start, end, step, ok) {
|
2013-08-27 15:14:44 +04:00
|
|
|
ok = 0
|
|
|
|
split(ctval, parts, /,/)
|
|
|
|
for(i = 1; i <= length(parts); i++) {
|
|
|
|
part = parts[i]
|
|
|
|
start = -1; end = -1; step = 1
|
|
|
|
# extraire /step
|
|
|
|
pos = index(part, "/")
|
2015-08-20 07:58:17 +04:00
|
|
|
if (pos != 0) {
|
2013-08-27 15:14:44 +04:00
|
|
|
step = substr(part, pos + 1) + 0
|
|
|
|
part = substr(part, 1, pos - 1)
|
|
|
|
}
|
|
|
|
# traiter start-end
|
|
|
|
pos = index(part, "-")
|
|
|
|
if (pos != 0) {
|
|
|
|
start = substr(part, 1, pos - 1) + 0
|
|
|
|
end = substr(part, pos + 1) + 0
|
|
|
|
} else {
|
|
|
|
start = part + 0
|
|
|
|
end = start
|
|
|
|
}
|
|
|
|
# tester si ça matche
|
|
|
|
for (j = start; j <= end; j += step) {
|
|
|
|
if (j == ref) {
|
|
|
|
ok = 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
#print " step: is " ref " in [" start "-" end "/" step "] ? " (ok? "yes": "no") #DEBUG
|
2013-08-27 15:14:44 +04:00
|
|
|
if (ok) break
|
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
#print "final: is " ref " ~ " ctval " ? " (ok? "yes": "no") #DEBUG
|
2013-08-27 15:14:44 +04:00
|
|
|
return ok
|
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
function ctmatch_all(M, H, dom, mon, dow, refM, refH, refdom, refmon, refdow, Mok, Hok, domok, monok, dowok) {
|
2017-02-20 13:55:49 +04:00
|
|
|
if (refM == "**ANY**") return 1
|
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
gsub(/\*/, "0-59", M)
|
|
|
|
Mok = ctmatch_one(M, refM)
|
|
|
|
|
|
|
|
gsub(/\*/, "0-23", H)
|
|
|
|
Hok = ctmatch_one(H, refH)
|
|
|
|
|
|
|
|
gsub(/\*/, "1-31", dom)
|
|
|
|
domok = ctmatch_one(dom, refdom)
|
|
|
|
|
|
|
|
gsub(/\*/, "1-12", mon)
|
|
|
|
monok = ctmatch_one(mon, refmon)
|
|
|
|
|
|
|
|
gsub(/\*/, "1-7", dow)
|
|
|
|
dowok = ctmatch_one(dow, refdow)
|
|
|
|
|
|
|
|
return Mok && Hok && monok && (domok || dowok)
|
|
|
|
}
|
|
|
|
function print_cmd(cmd) {
|
|
|
|
print "__ctexec " quote_value(cmd)
|
|
|
|
}
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
refM = ctnow[1]; refH = ctnow[2]; refdom = ctnow[3]; refmon = ctnow[4]; refdow = ctnow[5]
|
|
|
|
ctref = refM " " refH " " refdom " " refmon " " refdow
|
2014-08-20 00:12:36 +04:00
|
|
|
print "## now: " ctref
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
# est-on dans une série de ctline?
|
|
|
|
ctline_run = 0
|
|
|
|
# dans une série de ctline, il y a-t-il au moins un match?
|
|
|
|
ctline_run_one_match = 0
|
|
|
|
# faut-il afficher les commandes indentées?
|
2013-10-02 14:11:47 +04:00
|
|
|
match_indented = 0
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
2014-04-23 19:30:36 +04:00
|
|
|
/^(export[ \t]+)?[a-zA-Z_][a-zA-Z0-9_]*[-+%#?]=/ {
|
2014-08-20 00:12:36 +04:00
|
|
|
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
|
|
|
|
|
2013-10-02 14:11:47 +04:00
|
|
|
# manipulation de variables de type PATH
|
2014-04-23 19:30:36 +04:00
|
|
|
match($0, /^(export[ \t]+)?([a-zA-Z_][a-zA-Z0-9_]*)([-+%#?])=(.*)$/, parts)
|
2013-10-02 13:37:10 +04:00
|
|
|
name = parts[2]
|
|
|
|
type = parts[3]
|
|
|
|
value = parts[4]
|
2013-08-27 15:14:44 +04:00
|
|
|
if (type == "+" || type == "%") {
|
|
|
|
print "uaddpath " value " " name
|
|
|
|
} else if (type == "#") {
|
|
|
|
print "uinspath " value " " name
|
|
|
|
} else if (type == "-") {
|
|
|
|
print "udelpath " value " " name
|
2014-04-23 19:30:36 +04:00
|
|
|
} else if (type == "?") {
|
|
|
|
print "[ -n \"$" name "\" ] || " name "=" value
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
2013-10-02 13:37:10 +04:00
|
|
|
print "export " name
|
2013-08-27 15:14:44 +04:00
|
|
|
next
|
|
|
|
}
|
2013-10-02 13:37:10 +04:00
|
|
|
/^(export[ \t]+)?[a-zA-Z_][a-zA-Z0-9_]*=/ {
|
2014-08-20 00:12:36 +04:00
|
|
|
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
|
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
# spécification de variable
|
2013-10-02 13:37:10 +04:00
|
|
|
sub(/^export[ \t]+/, "", $0)
|
2013-08-27 15:14:44 +04:00
|
|
|
print "export " $0
|
|
|
|
next
|
|
|
|
}
|
2013-10-02 13:37:10 +04:00
|
|
|
/^\$.+/ {
|
2014-08-20 00:12:36 +04:00
|
|
|
ctline_run = 0; ctline_run_one_match = 0; match_indented = 0
|
|
|
|
|
2013-10-02 13:37:10 +04:00
|
|
|
# exécution de commande arbitraire
|
2013-11-15 13:27:18 +04:00
|
|
|
if ($0 ~ /^\$\{([ \t]*(#.*)?)?$/) {
|
|
|
|
# commande sur plusieurs lignes
|
|
|
|
getline
|
|
|
|
while ($0 !~ /^\$\}([ \t]*(#.*)?)?$/) {
|
|
|
|
print
|
|
|
|
if (getline <= 0) break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# commande sur une seule ligne
|
|
|
|
sub(/^\$/, "", $0)
|
|
|
|
print
|
|
|
|
}
|
2013-10-02 13:37:10 +04:00
|
|
|
next
|
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
|
2013-10-02 13:37:10 +04:00
|
|
|
/^[ \t]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+[ ]*[-0-9/*,]+/ {
|
2014-08-20 00:12:36 +04:00
|
|
|
match_indented = 0
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
M = $1; H = $2; dom = $3; mon = $4; dow = $5
|
2013-08-27 15:14:44 +04:00
|
|
|
ctline = M " " H " " dom " " mon " " dow
|
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
sub(/^[ \t]*[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]+[^ ]+[ ]*/, "", $0)
|
|
|
|
cmd = $0
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
match_cur = ctmatch_all(M, H, dom, mon, dow, refM, refH, refdom, refmon, refdow)
|
|
|
|
if (match_cur) print "## match: " ctline
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
if (cmd == "") {
|
|
|
|
ctline_run = 1
|
|
|
|
if (match_cur) ctline_run_one_match = 1
|
|
|
|
if (ctline_run_one_match) match_indented = 1
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
} else {
|
|
|
|
if (ctline_run && match_cur) ctline_run_one_match = 1
|
|
|
|
if (ctline_run && ctline_run_one_match) {
|
|
|
|
print_cmd(cmd)
|
|
|
|
match_indented = 1
|
|
|
|
}
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
if (!ctline_run && match_cur) {
|
|
|
|
print_cmd(cmd)
|
|
|
|
match_indented = 1
|
|
|
|
}
|
2013-08-27 15:14:44 +04:00
|
|
|
|
2014-08-20 00:12:36 +04:00
|
|
|
ctline_run = 0
|
|
|
|
ctline_run_one_match = 0
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 14:11:47 +04:00
|
|
|
next
|
|
|
|
}
|
|
|
|
/^[ \t]+/ {
|
2014-08-20 00:12:36 +04:00
|
|
|
ctline_run = 0; ctline_run_one_match = 0
|
|
|
|
|
2013-10-02 14:11:47 +04:00
|
|
|
if (match_indented) {
|
|
|
|
sub(/^[ \t]+/, "", $0)
|
2014-08-20 00:12:36 +04:00
|
|
|
print_cmd($0)
|
2013-08-27 15:14:44 +04:00
|
|
|
}
|
2014-08-20 00:12:36 +04:00
|
|
|
|
2013-08-27 15:14:44 +04:00
|
|
|
next
|
|
|
|
}
|
|
|
|
{
|
|
|
|
print "__cterror " quote_value("ligne de format incorrect: " $0)
|
|
|
|
}
|
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
## Préfixes
|
|
|
|
|
|
|
|
function get_default_crontabdir_prefix() {
|
|
|
|
if check_sysinfos -s darwin; then
|
|
|
|
if check_sysinfos -d 10.6+; then
|
|
|
|
echo /var/at/tabs
|
|
|
|
else
|
|
|
|
echo /var/cron/tabs
|
|
|
|
fi
|
|
|
|
elif check_sysinfos -s linux; then
|
|
|
|
if check_sysinfos -d debianlike; then
|
|
|
|
echo /var/spool/cron/crontabs
|
|
|
|
elif check_sysinfos -d redhatlike; then
|
|
|
|
echo /var/spool/cron
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
__crontab_prefixes=
|
|
|
|
function __compute_crontab_prefixes() {
|
|
|
|
[ -n "$__crontab_prefixes" ] && return
|
|
|
|
CRONTABDIR="${CRONTABDIR:-$(get_default_crontabdir_prefix)}"
|
|
|
|
__crontab_prefixes=1
|
|
|
|
}
|
|
|
|
|
2015-08-23 22:56:23 +04:00
|
|
|
CRONTAB_PREFIXES=(CRONTABDIR)
|
|
|
|
UTOOLS_PREFIXES=("${UTOOLS_PREFIXES[@]}" "${CRONTAB_PREFIXES[@]}")
|
2013-08-27 15:14:44 +04:00
|
|
|
|
|
|
|
function compute_crontab_prefixes() {
|
|
|
|
__compute_crontab_prefixes
|
|
|
|
}
|
|
|
|
|
|
|
|
function recompute_crontab_prefixes() {
|
2015-08-23 22:56:23 +04:00
|
|
|
local v
|
|
|
|
for v in "${CRONTAB_PREFIXES[@]}"; do
|
|
|
|
eval "$v="
|
|
|
|
done
|
2013-08-27 15:14:44 +04:00
|
|
|
__crontab_prefixes=
|
|
|
|
__compute_crontab_prefixes
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_CRONTABDIR_prefix() {
|
|
|
|
__compute_crontab_prefixes
|
|
|
|
echo "$CRONTABDIR"
|
|
|
|
}
|