Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
04-linux:20-bash:30-exemples [2016/11/10 00:58] – [Trap + kill exemple] Roge | 04-linux:20-bash:30-exemples [2017/10/06 23:40] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Exemples ====== | ||
+ | ===== Tester une durée d' | ||
+ | |||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | echo " | ||
+ | echo "--- Test duration (2s) ---" | ||
+ | echo " | ||
+ | TIME_START=`echo " | ||
+ | TIME_START_1970=$(echo " | ||
+ | sleep 2 | ||
+ | TIME_END=$(echo " | ||
+ | TIME_END_1970=`echo " | ||
+ | echo "---- Start: $TIME_START, | ||
+ | </ | ||
+ | |||
+ | ===== Trap + kill exemple ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/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 " | ||
+ | } | ||
+ | |||
+ | function clean_up { | ||
+ | |||
+ | # Perform program exit housekeeping | ||
+ | # Optionally accepts an exit status | ||
+ | echo -e ' | ||
+ | exit $1 | ||
+ | } | ||
+ | |||
+ | function error_exit { | ||
+ | |||
+ | # Display error message and exit | ||
+ | echo " | ||
+ | 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 ' | ||
+ | echo -e "\n >>> | ||
+ | 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 | ||
+ | </ | ||
+ | |||
+ | ===== Trap + kill exemple ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/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 | ||
+ | | ||
+ | |||
+ | # Perform program exit hxit status | ||
+ | echo -e ' | ||
+ | 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 ' | ||
+ | } | ||
+ | |||
+ | # 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 " | ||
+ | kill $! | ||
+ | </ | ||
+ | |||
+ | ===== Save full wiki ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | TIME_START_1970=$(date +%s) | ||
+ | LOG_DIR=/ | ||
+ | BUREAU=" | ||
+ | |||
+ | if [ $(hostname) == " | ||
+ | then | ||
+ | LOGFILE=" | ||
+ | REMOTE_DIR=" | ||
+ | | ||
+ | | ||
+ | TMP_DIR="/ | ||
+ | | ||
+ | |||
+ | echo "FULL Web backup: HAL <-- http:// | ||
+ | TIME_START=$(date +%T) | ||
+ | TIME_START_1970=$(date +%s) | ||
+ | |||
+ | | ||
+ | rm -Rf $TMP_DIR | ||
+ | mkdir $TMP_DIR | ||
+ | # Mesure de sécurité (à cause des rm) | ||
+ | cd $TMP_DIR || { | ||
+ | echo "*** ERREUR | ||
+ | exit -1 | ||
+ | } | ||
+ | |||
+ | # -c, --continue : continue a mirror job if possible | ||
+ | # -R, --reverse : reverse mirror (put files) ==> Upload | ||
+ | # -e, --delete | ||
+ | # -n, --only-newer : | ||
+ | |||
+ | lftp -c " | ||
+ | set ftp: | ||
+ | open ftp:// | ||
+ | cd $REMOTE_DIR; | ||
+ | lcd $TMP_DIR; | ||
+ | mirror --continue --parallel=8 --log=$LOGFILE | ||
+ | close; | ||
+ | quit; | ||
+ | " >> $LOGFILE | ||
+ | |||
+ | # Création de l' | ||
+ | zip -qr " | ||
+ | echo -e "File created: $zipTarget [$(du -h $zipTarget | ||
+ | cd $local_dir | ||
+ | rm -Rf $TMP_DIR | ||
+ | # Nettoyage du dossier des archives | ||
+ | | ||
+ | # Nettoyage des logs | ||
+ | | ||
+ | |||
+ | | ||
+ | echo -e " | ||
+ | echo -e " | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | </ | ||
+ | |||
+ | ===== cleanArchiveDir.sh ===== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | usage() { | ||
+ | cat << | ||
+ | |||
+ | 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 " | ||
+ | while [[ $# > 0 ]] | ||
+ | do | ||
+ | # echo -e "$# - OPTION: $1 + $2" | ||
+ | case $1 in | ||
+ | -t|--target) | ||
+ | if [ " | ||
+ | | ||
+ | shift 2 | ||
+ | else | ||
+ | | ||
+ | fi | ||
+ | ;; | ||
+ | -p|--prefix ) | ||
+ | if [ " | ||
+ | | ||
+ | shift 2 | ||
+ | else | ||
+ | | ||
+ | fi | ||
+ | ;; | ||
+ | -s|--suffix ) | ||
+ | if [ " | ||
+ | | ||
+ | shift 2 | ||
+ | else | ||
+ | | ||
+ | fi | ||
+ | ;; | ||
+ | -k|--keep) | ||
+ | if [ " | ||
+ | | ||
+ | shift 2 | ||
+ | else | ||
+ | | ||
+ | fi | ||
+ | ;; | ||
+ | *) # Unknown option | ||
+ | echo -e "Error : Unknown option $1\n" | ||
+ | usage | ||
+ | exit -1 | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | # echo " | ||
+ | # echo " | ||
+ | # echo " | ||
+ | |||
+ | # le dossier cible doit être spécifiée | ||
+ | [ ! " | ||
+ | echo -e "Error -t: a directory must be specified" | ||
+ | usage | ||
+ | exit -1 | ||
+ | } | ||
+ | |||
+ | # Le targetDir doit exister | ||
+ | [[ ! -d " | ||
+ | echo -e " | ||
+ | usage | ||
+ | exit -1 | ||
+ | } | ||
+ | |||
+ | # le prefix ou le suffix | ||
+ | [ ! " | ||
+ | echo -e " | ||
+ | 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/ | ||
+ | # echo " | ||
+ | [ ! " | ||
+ | echo -e " | ||
+ | usage | ||
+ | exit -1 | ||
+ | } | ||
+ | |||
+ | # echo " | ||
+ | aSupprimer=$(expr $nbArchives - $keep_archives) | ||
+ | count=0 | ||
+ | [ $aSupprimer -lt 1 ] && { | ||
+ | echo "Il n'y a pas d' | ||
+ | exit 0 | ||
+ | } | ||
+ | echo " | ||
+ | |||
+ | for ar in $targetDir/ | ||
+ | if [ $count -lt $aSupprimer ] | ||
+ | then | ||
+ | echo " | ||
+ | rm -f $ar | ||
+ | else | ||
+ | echo " | ||
+ | fi | ||
+ | | ||
+ | done | ||
+ | </ |