diff --git a/uscrontab b/uscrontab index dce90c6..85bc28b 100755 --- a/uscrontab +++ b/uscrontab @@ -177,11 +177,15 @@ OPTIONS -e, --edit Lancer un editeur pour modifier la crontab spécifiée. Si aucun fichier n'est spécifié, éditer $USCRONTAB_USERFILE + -r, --remove + Supprimer le fichier $USCRONTAB_USERFILE s'il existe + Si l'argument /path/to/crontab est spécifié, il est ignoré. -l, --list Si l'argument /path/to/crontab est spécifié, afficher le contenu de ce fichier. Sinon, lister les contenus des fichiers crontab qui sont exécutés avec la planification actuelle. Si une planification générique - est installée, afficher le contenu du fichier + est installée, ou si aucune planification n'est en cours, afficher le + contenu du fichier $USCRONTAB_USERFILE et chacun des fichiers du répertoire $USCRONTAB_USERDIR @@ -215,6 +219,16 @@ OPTIONS AVANCEES le traitement." } +function set_usercrontabs() { + # initialiser le tableau $1(=usercrontabs) avec la liste suivante: le + # fichier $USCRONTAB_USERFILE s'il existe, puis la liste des fichiers dans + # le répertoire $USCRONTAB_USERDIR + local -a _userfile _userdir + [ -f "$USCRONTAB_USERFILE" ] && _userfile=("$USCRONTAB_USERFILE") + array_lsfiles _userdir "$USCRONTAB_USERDIR" + eval "${1:-usercrontabs}"'=("${_userfile[@]}" "${_userdir[@]}")' +} + USCRONTAB_CTLINE="* * * * * $script" USCRONTAB_LOCKDELAY=8 USCRONTAB_STOPEC=101 @@ -238,43 +252,93 @@ parse_opts "${PRETTYOPTS[@]}" \ -c,--continuous continuous=1 \ -k:,--stop: USCRONTAB_STOPEC= \ -l,--list action=list \ + -e,--edit action=edit \ + -r,--remove action=remove \ @ args -- "$@" && set -- "${args[@]}" || die "$args" -if [ "$action" == "list" ]; then - crontab="$1"; shift - [ -n "$crontab" ] && crontab="$(abspath "$crontab")" +crontab="$1"; shift - array_from_lines ctfiles "$(crontab -l 2>/dev/null | awkrun script="$script" '$6 == script { print $7 }')" - found= - for ctfile in "${ctfiles[@]}"; do - if [ -z "$crontab" -o "$ctfile" == "$crontab" ]; then - found=1 - etitle "$(ppath "$ctfile")" - cat "$ctfile" - eend - fi - done - if [ -n "$crontab" -a -z "$found" ]; then - ewarn "$(ppath "$crontab"): non planifié" +if [ "$action" == "edit" ]; then + if [ -z "$crontab" ]; then + basedir="$(dirname "$USCRONTAB_USERFILE")" + [ -d "$basedir" ] || die "$basedir: ce répertoire n'existe pas. Vérifiez l'installation de nutools" + crontab="$USCRONTAB_USERFILE" + fi + enote "Edition de $crontab" + "${EDITOR:-vi}" "$crontab" + exit 0 + +elif [ "$action" == "remove" ]; then + [ -n "$crontab" ] && ewarn "$crontab: cet argument a été ignoré" + crontab="$USCRONTAB_USERFILE" + if [ -f "$crontab" ]; then + ask_yesno "Voulez-vous supprimer le fichier $crontab?" C || die + enote "Suppression de $crontab" + rm "$crontab" || die fi exit 0 + +elif [ "$action" == "list" ]; then + if [ -n "$crontab" ]; then + crontab="$(abspath "$crontab")" + array_from_lines ctfiles "$(crontab -l 2>/dev/null | awkrun script="$script" crontab="$crontab" '$6 == script && $7 == crontab { print $7 }')" + if [ ${#ctfiles[*]} -eq 0 ]; then + ewarn "$(ppath "$crontab"): non planifié" + ctfiles=("$crontab") + fi + else + array_from_xlines ctfiles "$(crontab -l 2>/dev/null | awkrun script="$script" '$6 == script { print $7 }')" + if array_contains ctfiles ""; then + # il y a une planification générique + array_del ctfiles "" + set_usercrontabs usercrontabs + array_extend ctfiles usercrontabs + elif [ ${#ctfiles[*]} -eq 0 ]; then + ewarn "aucune planification en cours" + set_usercrontabs ctfiles + fi + fi + + r=1 + for ctfile in "${ctfiles[@]}"; do + r=0 # il y a au moins une planification + etitle "$(ppath "$ctfile")" \ + cat "$ctfile" + done + exit $r fi -crontab="$1"; shift -[ -n "$crontab" ] || die_with "Vous devez spécifier le fichier crontab" display_help -[ -f "$crontab" ] || die "$crontab: fichier introuvable" -crontab="$(abspath "$crontab")" +[ -z "$crontab" -o -f "$crontab" ] || die "$crontab: fichier introuvable" +[ -n "$crontab" ] && crontab="$(abspath "$crontab")" if [ "$action" == "install" ]; then - enable_in_crontab "$USCRONTAB_CTLINE $(quoted_arg "$crontab")" && estep "add_to_crontab $USCRONTAB_CTLINE $(quoted_arg "$crontab")" + ctline="$USCRONTAB_CTLINE" + [ -n "$crontab" ] && ctline="$ctline $(quoted_arg "$crontab")" + enable_in_crontab "$ctline" && estep "add_to_crontab $ctline" elif [ "$action" == "uninstall" ]; then - remove_from_crontab "$USCRONTAB_CTLINE $(quoted_arg "$crontab")" && estep "remove_from_crontab $USCRONTAB_CTLINE $(quoted_arg "$crontab")" + ctline="$USCRONTAB_CTLINE" + [ -n "$crontab" ] && ctline="$ctline $(quoted_arg "$crontab")" + remove_from_crontab "$ctline" && estep "remove_from_crontab $ctline" elif [ "$action" == "run" ]; then + clean_crontab= + if [ -n "$crontab" ]; then + default_lockfile="/var/run/$scriptname$crontab.lock" + else + set_usercrontabs usercrontabs + ac_set_tmpfile crontab + clean_crontab=1 + for usercrontab in "${usercrontabs[@]}"; do + echo "# $usercrontab" >>"$crontab" + cat "$usercrontab" >>"$crontab" + done + default_lockfile="/var/run/$scriptname$USCRONTAB_USER.lock" + fi + if [ "$lockfile" == auto ]; then if is_root; then - lockfile="/var/run/$scriptname$crontab.lock" + lockfile="$default_lockfile" mkdirof "$lockfile" || die else lockfile= @@ -284,6 +348,8 @@ elif [ "$action" == "run" ]; then if [ -n "$lockfile" ]; then lockwarn="${lockfile%.lock}.lockwarn" + autoclean "$lockwarn" + retry=1 while [ -n "$retry" ]; do case "$(lf_trylock -h "$lockdelay" "$lockfile")" in @@ -306,7 +372,8 @@ elif [ "$action" == "run" ]; then *) retry=;; esac done - [ -f "$lockwarn" ] && rm "$lockwarn" + + ac_clean "$lockwarn" autoclean "$lockfile" fi @@ -367,6 +434,6 @@ puis supprimez le cas échéant le fichier $1" eval "$ctscript" ac_cleanall ); ec=$? - [ -f "$lockfile" ] && rm "$lockfile" + ac_clean "$lockfile" exit "$ec" fi