#!/bin/bash echo "--------------------------" echo "--- Test duration (2s) ---" echo "--------------------------" TIME_START=`echo "$(date +%T)"` TIME_START_1970=$(echo "$(date +%s)") sleep 2 TIME_END=$(echo "$(date +%T)") TIME_END_1970=`echo "$(date +%s)"` echo "---- Start: $TIME_START, End: $TIME_END, duration $(echo $TIME_END_1970 - $TIME_START_1970 | bc -l) ---"
#!/bin/bash # Usage: bashTrapExemple # Create process that kills this process after N seconds. # The current process traps the kill signal and does some clean up. # The current process can be killed with ^C ou kill PID function usage { # Display usage message on standard error echo "Usage: $PROGNAME file" 1>&2 } function clean_up { # Perform program exit housekeeping # Optionally accepts an exit status echo -e '\n--> clean-up done !!!' exit $1 } function error_exit { # Display error message and exit echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2 clean_up 1 } # Cette fonction a pour but de tuer un process au bout de n secondes function kill_this_within_delay { # Display error message and exit sleep 8s echo -e '\a' echo -e "\n >>> Killing $1" kill $1 } # on lance en background un processus qui tue le process courant au bout de 3 secondes kill_this_within_delay $$ & # recupération des signaux pour traitement trap clean_up SIGHUP SIGINT SIGTERM SIGKILL # begin of treatment echo "PID du processus courant : $$" echo "PID du processus lancé : $!" echo Endless loop started! while : # This is the same as "while true". do sleep 0.1 # This script is not really doing anything. echo -n . done
#!/bin/bash echo "PID du processus courant : $$" function dummy_loop { # La fonction clean_up permet de reprendre la main après le kill du script function clean_up { # Perform program exit hxit status echo -e '\ndummy_loop --> clean-up done !!!\n' exit $1 } trap clean_up SIGHUP SIGINT SIGTERM SIGKILL # Looping.... while : # This is the same as "while true". do sleep 0.05 # This script is not really doing anything. echo -n . done echo -e '\ndummy_loop --> FIN' # Ne sera jamais affiché } # On lance une commande en background avec => & # Ca peut être une commande, un autre script bash, un programme, etc... dummy_loop & echo "PID du processus lancé (dummy_loop) : $!" sleep 3 echo -e "\nKilling $! ..." kill $!
#!/bin/bash TIME_START_1970=$(date +%s) LOG_DIR=/var/log/ BUREAU="HAL" if [ $(hostname) == "$BUREAU" ] then LOGFILE="${LOG_DIR}$(basename $0).$(date +%Y\-%m\-%d\-%Hh%Mmn%S).log" REMOTE_DIR="./www/wiki-2014-11-13/" archiveDir="/cygdrive/f/Z----Sauveqardes----Z/SpecialBackup/" archiveName="wiki.roge.info-FULL" TMP_DIR="/tmp/${archiveName}/" zipTarget="${archiveDir}${archiveName}-$(date +%Y\-%m\-%d\-%Hh%Mmn%S).zip" echo "FULL Web backup: HAL <-- http://roge.info/wiki" | tee -a $LOGFILE TIME_START=$(date +%T) TIME_START_1970=$(date +%s) local_dir=$(pwd) rm -Rf $TMP_DIR mkdir $TMP_DIR # Mesure de sécurité (à cause des rm) cd $TMP_DIR || { echo "*** ERREUR *** cd $TMP_DIR " | tee -a $LOGFILE exit -1 } # -c, --continue : continue a mirror job if possible # -R, --reverse : reverse mirror (put files) ==> Upload # -e, --delete : delete files not present at remote site # -n, --only-newer : download only newer files (-c won't work) lftp -c " set ftp:list-options -a; open ftp://user:pass@ftp.site; cd $REMOTE_DIR; lcd $TMP_DIR; mirror --continue --parallel=8 --log=$LOGFILE close; quit; " >> $LOGFILE # Création de l'archive zip -qr "$zipTarget" * | tee -a $LOGFILE echo -e "File created: $zipTarget [$(du -h $zipTarget | cut -f 1)]" cd $local_dir rm -Rf $TMP_DIR # Nettoyage du dossier des archives cleanArchiveDir.sh -t $archiveDir -p $archiveName -k 7 | tee -a $LOGFILE # Nettoyage des logs cleanArchiveDir.sh -t ${LOG_DIR} -p $(basename $0) -s log -k 3 | tee -a $LOGFILE TIME_END_1970=$(date +%s) echo -e "\n******** $(date +%T) - $(hostname): Fin de $(basename $0) OK! ($(convertDuration.sh $(expr $TIME_END_1970 - $TIME_START_1970))) **********" | tee -a $LOGFILE echo -e "\nlog: $LOGFILE" else echo "ERROR: This script runs only on $BUREAU host" fi
#!/bin/bash usage() { cat <<-'_usage_statement_' cleanArchiveDir.sh supprime des archives pour ne conserver que les 3 derniers Usage: cleanArchiveDir.sh -t directory -p prefix -s suffix [ -k number] -s, --suffix naming suffix for the archives (a prefix and/or a suffix must be specified) -p, --prefix naming prefix for the archives (a prefix and/or a suffix must be specified) -t, --target directory for the final archive (must exist) -k, --keep number of archives to be kept _usage_statement_ } keep_archives=3 # default value # echo -e "OPTIONS: $# - $@" while [[ $# > 0 ]] do # echo -e "$# - OPTION: $1 + $2" case $1 in -t|--target) if [ "$2" ] ; then # $2 exists targetDir=$2; shift 2 else shift fi ;; -p|--prefix ) if [ "$2" ] ; then # $2 exists prefix=$2; shift 2 else shift fi ;; -s|--suffix ) if [ "$2" ] ; then # $2 exists suffix=$2; shift 2 else shift fi ;; -k|--keep) if [ "$2" ] ; then # $2 exists keep_archives=$2; shift 2 else shift fi ;; *) # Unknown option echo -e "Error : Unknown option $1\n" usage exit -1 ;; esac done # echo "targetDir=$targetDir" # echo "prefix=$prefix" # echo "keep_archives=$keep_archives" # le dossier cible doit être spécifiée [ ! "${targetDir}" ] && { echo -e "Error -t: a directory must be specified" usage exit -1 } # Le targetDir doit exister [[ ! -d "$targetDir" ]] && { echo -e "\nERREUR -t: the directory $targetDir does not exist!\n" usage exit -1 } # le prefix ou le suffix doit être spécifiée [ ! "${prefix}" ] && [ ! "${suffix}" ] && { echo -e "Error: a prefix and/or a suffix must be specified" usage exit -1 } # keep_archives doit être un entier [[ $keep_archives = +([0-9]) ]] || { echo "ERROR -k: $keep_archives must be an integer!" usage exit -1 } nbArchives=$(ls $targetDir/$prefix*$suffix | wc -l) # echo "nbArchives=$nbArchives" [ ! "${nbArchives}" ] && { echo -e "Error: * is forbiden in parameters" usage exit -1 } # echo "keep_archives=$keep_archives" aSupprimer=$(expr $nbArchives - $keep_archives) count=0 [ $aSupprimer -lt 1 ] && { echo "Il n'y a pas d'archive à supprimer ($nbArchives archive(s))" exit 0 } echo "Nettoyage des ${nbArchives} archives du dossier $targetDir " for ar in $targetDir/$prefix*$suffix ; do if [ $count -lt $aSupprimer ] then echo " - $(basename $ar), $(du -h $ar | cut -f 1) - deleted" rm -f $ar else echo " + $(basename $ar), $(du -h $ar | cut -f 1) - kept" fi ((count++)) done